百度地图接口开发

baidumap.html
<script>

var is_location = 0; //获取所在地的经纬度
 function getLocation() {
  if (navigator.geolocation) {  
   navigator.geolocation.getCurrentPosition(showPosition);
  }
 }
 function showPosition(position) {
  //根据IP定位http://api.map.baidu.com/location/ip?ak=lWp1O5BZuqVI6aLsZobgfAvq&ip=202.198.16.3&coor=bd09ll
  //根据地理位置http://api.map.baidu.com/geocoder/v2/?ak=lWp1O5BZuqVI6aLsZobgfAvq&callback=renderReverse&location=29.554765397706273,106.4602094533912&output=json
  location.href = '/?lng=' + position.coords.longitude + '&lat=' + position.coords.latitude;
    //aaa='/?lng=' + position.coords.longitude + '&lat=' + position.coords.latitude;
 }

 if (is_location == 0) {
  getLocation();
  
 }

</script>

 

 

baiduinter.php

<?php

//根据用户经纬度获取具体信息
function getXyLocation($longitude,$latitude){
    $url="http://api.map.baidu.com/geocoder/v2/?ak=lWp1O5BZuqVI6aLsZobgfAvq&callback=renderReverse&location=$latitude,$longitude&output=json";
    $file_contents = curl_get_contents($url);
    $strstart=strpos($file_contents,'(');
    $strend=strpos($file_contents,')');//var_dump($strend);die($strstart);
    $file_contents=substr($file_contents,$strstart+1,$strend-$strstart-1);
    $content = json_decode($file_contents);
 return object2array($content);
}

$xyinfo=getXyLocation($lng,$lat));

print($xyinfo);

?>

 

你可能感兴趣的:(百度地图接口)