百度API经纬度和地址互相查询

  1. /** 
  2.  * 获取地址对应的坐标 
  3.  * @param $address 
  4.  * @return array 
  5.  */  
  6. function getAddressPoint($address){  
  7.     $lng = 0;  
  8.     $lat = 0;  
  9.     $url = 'http://api.map.baidu.com/geocoder?output=json&address=' . urlencode($address);  
  10.     if(function_exists('curl_init')) {  
  11.         $ch = curl_init();  
  12.         curl_setopt($ch, CURLOPT_URL, $url);  
  13.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  14.         curl_setopt($ch, CURLOPT_HEADER, 0);  
  15.         $data = curl_exec($ch);  
  16.         curl_close($ch);  
  17.     }else{  
  18.         $data = file_get_contents($url,false,stream_context_create(array(  
  19.             "http"=>array(  
  20.                 "method"=>"GET",  
  21.                 "timeout"=>1  
  22.             ),  
  23.         )));  
  24.     }  
  25.   
  26.     $data = json_decode($data,true);  
  27.     if($data && $data['status'] == 'OK' && isset($data['result']) && isset($data['result']['location']))  
  28.     {  
  29.         $lng = $data['result']['location']['lng'];  
  30.         $lat = $data['result']['location']['lat'];  
  31.     }  
  32.     return array($lng,$lat);  
  33. }  
  34.   
  35. /** 
  36.  * 逆地理编码专属请求 
  37.  * User: Lg 
  38.  * Date: 2016/4/11 
  39.  * @param $address 
  40.  * @return array 
  41.  */  
  42. function getAddress($lat,$lng){  
  43.     $location = $lat.','.$lng;  
  44.     $url = 'http://api.map.baidu.com/geocoder?callback=renderReverse&location='.$location.'&output=json&pois=1';  
  45.     if(function_exists('curl_init')) {  
  46.         $ch = curl_init();  
  47.         curl_setopt($ch, CURLOPT_URL, $url);  
  48.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  49.         curl_setopt($ch, CURLOPT_HEADER, 0);  
  50.         $data = curl_exec($ch);  
  51.         curl_close($ch);  
  52.     }else{  
  53.         $data = file_get_contents($url,false,stream_context_create(array(  
  54.             "http"=>array(  
  55.                 "method"=>"GET",  
  56.                 "timeout"=>1  
  57.             ),  
  58.         )));  
  59.     }  
  60.   
  61.     $data = json_decode($data,true);  
  62.     if($data && $data['status'] == 'OK' && isset($data['result']) && isset($data['result']['addressComponent']))  
  63.     {  
  64.         $province = $data['result']['addressComponent']['province'];  
  65.         $city = $data['result']['addressComponent']['city'];  
  66.         $district = $data['result']['addressComponent']['district'];  
  67.     }  
  68.     return array($province,$city,$district);  

转载于:https://www.cnblogs.com/sunwenshuo/p/8855693.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值