Google Map中Geocoder接口來反向地址解析
如下圖,我輸入了“天河美好居”,就可以反向解析到我的具體位置)
下面就班門弄斧的講述一下:
1. 引入所需的JS:
- <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
- <script type="text/javascript" src="JS/jquery-1.7.min.js"></script>
2. Javascript代碼:
- <script language="javascript" type="text/javascript">
- var map, marker,
- //實例化地址解析器
- geoCoder = new google.maps.Geocoder(),
- //初始化地圖的配置
- myOptions = {
- zoom: 15,
- center: new google.maps.LatLng(113.32152399999995, 23.134685),
- mapTypeId: google.maps.MapTypeId.ROADMAP
- };
- function GetGeoCoder() {
- var searchAddress = $("#txt_address").val();
- geoCoder.geocode({
- 'address': searchAddress
- },
- function(results, state) {
- if (state = google.maps.GeocoderStatus.OK) {
- if (results[0]) {
- var point = results[0].geometry.location;
- if (marker) {
- marker.setMap(null);
- }
- marker = new google.maps.Marker({
- map: map,
- position: point
- });
- var infowindow = new google.maps.InfoWindow({
- content: '<h3>我在這里.</h3>' + results[0].formatted_address
- });
- google.maps.event.addListener(marker, 'click',
- function() {
- infowindow.open(map, marker);
- });
- map.setCenter(point);
- }
- }
- })
- }
- //利用html5的geolocation來獲取當前位置
- function GetGeoLocation() {
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(function(position) {
- var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
- console.log(position);
- geoCoder.geocode({
- 'latLng': pos
- },
- function(results, state) {
- if (state = google.maps.GeocoderStatus.OK) {
- if (results[0]) {
- var point = results[0].geometry.location;
- var myDirection = results[0].formatted_address;
- if (marker) {
- marker.setMap(null);
- }
- marker = new google.maps.Marker({
- map: map,
- position: point
- });
- var infowindow = new google.maps.InfoWindow({
- content: '<h3>我在這里</h3>' + myDirection
- });
- google.maps.event.addListener(marker, 'click',
- function() {
- infowindow.open(map, marker);
- });
- map.setCenter(point);
- $("#txt_address").val(myDirection.split(' ')[0]);
- }
- }
- })
- },
- function() {
- handleNoGeolocation(true);
- },
- {
- 'enableHighAccuracy': true,
- 'timeout': 10000,
- 'maximumAge': 0
- });
- } else {
- // 瀏覽器不支持Geolocation
- handleNoGeolocation(false);
- }
- }
- function handleNoGeolocation(errorFlag) {
- if (errorFlag) {
- var content = 'Error: The Geolocation service failed.';
- } else {
- var content = 'Error: Your browser doesn\'t support geolocation.';
- }
- var options = {
- map: map,
- position: new google.maps.LatLng(113.32152399999995, 23.134685),
- content: content
- };
- var infowindow = new google.maps.InfoWindow(options);
- map.setCenter(options.position);
- }
- $(function() {
- map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
- //頁面加載時,利用html5獲取當前位置
- GetGeoLocation();
- $('#txt_address').bind('keyup',
- function(event) {
- if (event.keyCode == 13) {
- GetGeoCoder();
- }
- });
- });
- </script>
3. HTML Code:
- <input id="txt_address" class="input_css" type="text" value="Where Are You ?"/>
- <div id="map_canvas">
這樣就可以對地址進行反向解析了,這里只進行簡單的講述,對于Google API更多更詳細的描述,可以到Google Javascript API V3的官方文檔去了解。反向地址解析是很有用的,例如,公司數(shù)據(jù)庫中有上百萬的企業(yè)數(shù)據(jù),地址不夠完善,這時我們就可以利用它來幫助我們完善這些數(shù)據(jù)了。當然,Google Map嚴格限制了查詢的次數(shù),多次查詢會有LIMIT_QUERY的異常,這時我們就得使用一些手段來反限制,例如,在隨機的時間間隔后查詢等等,這里不詳述。對于企業(yè)應(yīng)用,還是付費使用,這樣更穩(wěn)定。
原文鏈接:http://www.cnblogs.com/three-zone/archive/2012/08/12/GeoCoder.html
【編輯推薦】