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. $data = json_decode($data,true);
  26. if($data && $data['status'] == 'OK' && isset($data['result']) && isset($data['result']['location']))
  27. {
  28. $lng = $data['result']['location']['lng'];
  29. $lat = $data['result']['location']['lat'];
  30. }
  31. return array($lng,$lat);
  32. }
  33. /**
  34. * 逆地理编码专属请求
  35. * User: Lg
  36. * Date: 2016/4/11
  37. * @param $address
  38. * @return array
  39. */
  40. function getAddress($lat,$lng){
  41. $location = $lat.','.$lng;
  42. $url = 'http://api.map.baidu.com/geocoder?callback=renderReverse&location='.$location.'&output=json&pois=1';
  43. if(function_exists('curl_init')) {
  44. $ch = curl_init();
  45. curl_setopt($ch, CURLOPT_URL, $url);
  46. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  47. curl_setopt($ch, CURLOPT_HEADER, 0);
  48. $data = curl_exec($ch);
  49. curl_close($ch);
  50. }else{
  51. $data = file_get_contents($url,false,stream_context_create(array(
  52. "http"=>array(
  53. "method"=>"GET",
  54. "timeout"=>1
  55. ),
  56. )));
  57. }
  58. $data = json_decode($data,true);
  59. if($data && $data['status'] == 'OK' && isset($data['result']) && isset($data['result']['addressComponent']))
  60. {
  61. $province = $data['result']['addressComponent']['province'];
  62. $city = $data['result']['addressComponent']['city'];
  63. $district = $data['result']['addressComponent']['district'];
  64. }
  65. return array($province,$city,$district);
  66. }
05-11 12:54