var geocoder = new google.maps.Geocoder();
var fenway = new google.maps.LatLng(37.4419, -122.1419);
var mapOptions = {
center : fenway,
zoom : 14,
mapTypeId : google.maps.MapTypeId.ROADMAP,
streetViewControl : true
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
function codeAddress(address) {
geocoder.geocode({
"address" : address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
// marker 标记
var marker = new google.maps.Marker({
map : map,
position : results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: "
+ status);
}
});
}
依据输入的地名定位到目标点并做标记。 代码很简单