2014-01-08 78 views
0

我一直在尋找Google maps dev網站上的地理編碼示例。我發現這個例子,並希望作爲一個實驗硬代碼郵政編碼,而不是使用輸入框來獲取地址(總體目標是讓xml在這裏放置郵政地址)。這只是一個硬編碼'地址'標籤內的郵政地址的情況嗎?我確實嘗試過但不喜歡。希望這是有道理的?Hardcode是用於地理編碼的郵政地址

<script> 
    var geocoder; 
    var map; 

    function initialize() { 

     geocoder = new google.maps.Geocoder(); 
     var latlng = new google.maps.LatLng(-34.397, 150.644); 

     var mapOptions = { 
      zoom: 8, 
      center: latlng 
     } 

     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    } 

    function codeAddress() { 

     var address = document.getElementById('address').value; 
     geocoder.geocode({ 'address': address}, function(results, status) { 

      if (status == google.maps.GeocoderStatus.OK) { 
       map.setCenter(results[0].geometry.location); 
       var marker = new google.maps.Marker({ 
        map: map, 
        position: results[0].geometry.location 
       }); 
      } else { 
       alert('Geocode was not successful for the following reason: ' + status); 
      } 

     }); 
    } 

    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 
</head> 
<body> 
    <div id="panel"> 
     <input id="address" type="textbox" value="Sydney, NSW"> 
     <input type="button" value="Geocode" onclick="codeAddress()"> 
    </div> 
    <div id="map-canvas"></div> 
</body> 

https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

+0

你嘗試VAR地址= 「NY」? –

回答

0

如果我深知,你可以嘗試這樣的:

http://jsfiddle.net/7g7qG/

var geocoder; 
var map; 
function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var mapOptions = { 
    zoom: 8, 
    center: latlng 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
    codeAddress('London') 
} 

function codeAddress(address) { 
    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 
    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 
+0

正是我需要的!我不是最擅長解釋問題的,但你明白,謝謝! – efc84

+0

附加問題,是否可以使用for循環而不是硬編碼地址?我有一個帶有郵編的xml文件,並希望使用相同的地理編碼打印地圖上的每個點,這是一個合適的方法來解決它嗎? – efc84

+0

看看這個:https://developers.google.com/maps/documentation/javascript/examples/map-projection-simple 可能幫助 – Ventura

相關問題