Google Map中Geocoder接口来反向地址解析

开发 开发工具
Google Map是个好东西,它提供的Geocoder接口可以对地址进行反向解析,从而得到诸如 “经纬度”,“国家”, “省”,“市”,“区”,“路” 等等的信息。

 如下图,我输入了“天河美好居”,就可以反向解析到我的具体位置)

下面就班门弄斧的讲述一下:

1. 引入所需的JS:

  1. <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> 
  2. <script type="text/javascript" src="JS/jquery-1.7.min.js"></script> 

2. Javascript代码:

  1. <script language="javascript" type="text/javascript"
  2.      var map, marker, 
  3.      //实例化地址解析器 
  4.      geoCoder = new google.maps.Geocoder(), 
  5.      //初始化地图的配置 
  6.      myOptions = { 
  7.          zoom: 15, 
  8.          center: new google.maps.LatLng(113.32152399999995, 23.134685), 
  9.          mapTypeId: google.maps.MapTypeId.ROADMAP 
  10.      }; 
  11.      function GetGeoCoder() { 
  12.          var searchAddress = $("#txt_address").val(); 
  13.          geoCoder.geocode({ 
  14.              'address': searchAddress 
  15.          }, 
  16.          function(results, state) { 
  17.              if (state = google.maps.GeocoderStatus.OK) { 
  18.                  if (results[0]) { 
  19.                      var point = results[0].geometry.location; 
  20.                      if (marker) { 
  21.                          marker.setMap(null); 
  22.                      } 
  23.                      marker = new google.maps.Marker({ 
  24.                          map: map, 
  25.                          position: point 
  26.                      }); 
  27.                      var infowindow = new google.maps.InfoWindow({ 
  28.                          content: '<h3>我在这里.</h3>' + results[0].formatted_address 
  29.                      }); 
  30.                      google.maps.event.addListener(marker, 'click'
  31.                      function() { 
  32.                          infowindow.open(map, marker); 
  33.                      }); 
  34.                      map.setCenter(point); 
  35.   
  36.                  } 
  37.              } 
  38.          }) 
  39.      } 
  40.      //利用html5的geolocation来获取当前位置 
  41.      function GetGeoLocation() { 
  42.          if (navigator.geolocation) { 
  43.              navigator.geolocation.getCurrentPosition(function(position) { 
  44.                  var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
  45.                  console.log(position); 
  46.                  geoCoder.geocode({ 
  47.                      'latLng': pos 
  48.                  }, 
  49.                  function(results, state) { 
  50.                      if (state = google.maps.GeocoderStatus.OK) { 
  51.                          if (results[0]) { 
  52.                              var point = results[0].geometry.location; 
  53.                              var myDirection = results[0].formatted_address; 
  54.                              if (marker) { 
  55.                                  marker.setMap(null); 
  56.                              } 
  57.                              marker = new google.maps.Marker({ 
  58.                                  map: map, 
  59.                                  position: point 
  60.                              }); 
  61.                              var infowindow = new google.maps.InfoWindow({ 
  62.                                  content: '<h3>我在这里</h3>' + myDirection 
  63.                              }); 
  64.                              google.maps.event.addListener(marker, 'click'
  65.                              function() { 
  66.                                  infowindow.open(map, marker); 
  67.                              }); 
  68.                              map.setCenter(point); 
  69.                              $("#txt_address").val(myDirection.split(' ')[0]); 
  70.   
  71.                          } 
  72.                      } 
  73.                  }) 
  74.              }, 
  75.              function() { 
  76.                  handleNoGeolocation(true); 
  77.              }, 
  78.              { 
  79.                  'enableHighAccuracy'true
  80.                  'timeout': 10000, 
  81.                  'maximumAge': 0 
  82.              }); 
  83.          } else { 
  84.              // 浏览器不支持Geolocation 
  85.              handleNoGeolocation(false); 
  86.          } 
  87.      } 
  88.   
  89.      function handleNoGeolocation(errorFlag) { 
  90.          if (errorFlag) { 
  91.              var content = 'Error: The Geolocation service failed.'
  92.          } else { 
  93.              var content = 'Error: Your browser doesn\'t support geolocation.'
  94.          } 
  95.   
  96.          var options = { 
  97.              map: map, 
  98.              position: new google.maps.LatLng(113.32152399999995, 23.134685), 
  99.              content: content 
  100.          }; 
  101.   
  102.          var infowindow = new google.maps.InfoWindow(options); 
  103.          map.setCenter(options.position); 
  104.      } 
  105.      $(function() { 
  106.          map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); 
  107.          //页面加载时,利用html5获取当前位置 
  108.          GetGeoLocation(); 
  109.          $('#txt_address').bind('keyup'
  110.          function(event) { 
  111.              if (event.keyCode == 13) { 
  112.                  GetGeoCoder(); 
  113.              } 
  114.          }); 
  115.      }); 
  116.  </script> 

3. HTML Code:

  1. <input id="txt_address" class="input_css" type="text" value="Where Are You ?"/> 
  2. <div id="map_canvas"> 

这样就可以对地址进行反向解析了,这里只进行简单的讲述,对于Google API更多更详细的描述,可以到Google Javascript API V3的官方文档去了解。反向地址解析是很有用的,例如,公司数据库中有上百万的企业数据,地址不够完善,这时我们就可以利用它来帮助我们完善这些数据了。当然,Google Map严格限制了查询的次数,多次查询会有LIMIT_QUERY的异常,这时我们就得使用一些手段来反限制,例如,在随机的时间间隔后查询等等,这里不详述。对于企业应用,还是付费使用,这样更稳定。

原文链接:http://www.cnblogs.com/three-zone/archive/2012/08/12/GeoCoder.html

【编辑推荐】

责任编辑:彭凡 来源: 博客园
相关推荐

2023-12-11 07:37:08

mongodb经纬度性能

2013-03-21 18:54:23

2011-02-22 09:40:18

HashMap

2012-06-05 09:54:50

Windows Pho

2010-06-18 15:33:19

UML接口

2010-10-25 09:06:47

Google Map应用

2013-12-20 10:36:56

2011-09-21 14:17:12

Google+

2010-02-06 14:19:26

ibmdwGoogleMap

2010-02-22 15:00:02

WCF信道工厂

2012-03-31 14:42:52

Google清明

2018-08-16 08:59:54

Google 云存储技术

2010-07-21 10:18:41

Perl map函数

2009-07-22 14:36:50

2016-11-14 19:45:39

JavaScript

2021-05-14 06:15:48

SpringAware接口

2011-05-20 16:33:47

委托接口

2018-01-08 17:17:46

微信

2011-09-07 10:08:40

Google

2020-02-25 16:00:28

JavaScript数据技术
点赞
收藏

51CTO技术栈公众号