addressTest.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="Generator" content="EditPlus®">
  6. <meta name="Author" content="">
  7. <meta name="Keywords" content="">
  8. <meta name="Description" content="">
  9. <title>Document</title>
  10. <script src="jquery-3.1.0.js"></script>
  11. <script type="text/javascript">
  12. <!--
  13. /**
  14. * 获取地址对应的坐标
  15. * @param $address
  16. * @return array
  17. */
  18. function getAddressPoint($address){
  19. $lng = 0;
  20. $lat = 0;
  21. $url = 'http://api.map.baidu.com/geocoder?output=json&address='.urlencode($address);
  22. if(function_exists('curl_init')) {
  23. $ch = curl_init();
  24. curl_setopt($ch, CURLOPT_URL, $url);
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  26. curl_setopt($ch, CURLOPT_HEADER, 0);
  27. $data = curl_exec($ch);
  28. curl_close($ch);
  29. }else{
  30. $data = file_get_contents($url,false,stream_context_create(array(
  31. "http"=>array(
  32. "method"=>"GET",
  33. "timeout"=>1
  34. ),
  35. )));
  36. }
  37. $data = json_decode($data,true);
  38. if($data && $data['status'] == 'OK' && isset($data['result']) && isset($data['result']['location']))
  39. {
  40. $lng = $data['result']['location']['lng'];
  41. $lat = $data['result']['location']['lat'];
  42. }
  43. return array($lng,$lat);
  44. }
  45. //-->
  46. </script>
  47. </head>
  48. <body>
  49. <button onclick="getAddressPoint('宛平南路75号')">获取经纬度</button>
  50. </body>
  51. </html>