【发布时间】:2014-01-26 12:57:15
【问题描述】:
我一直在查看 Google 地图开发网站上的地理编码示例。我找到了这个示例,并希望将邮政编码作为实验硬编码,而不是使用输入框来获取地址(总体目标是让 xml 将邮政地址放在此处)。这只是在“地址”标签内对邮政地址进行硬编码的情况吗?我确实尝试过,但没有高兴。希望这有意义吗?
<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
【问题讨论】:
-
你试过 var address = "NY" 吗?
标签: javascript google-maps google-maps-api-3 geocoding