sunwenshuo
  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);  

分类:

技术点:

相关文章: