代码只处理地址变量。 所以在select选项中有多个地址。
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('Select').value;
for(i = 0; i < address.length;i++){
geocoder.geocode({'address': address[i]}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
// resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[i].geometry.location
});
markers.push(marker);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
}
答案 0 :(得分:0)
从相关问题修改: Google Maps : change map marker location on dropdown
Set
var geocoder;
var map;
var geoMarker;
var bounds = new google.maps.LatLngBounds();
function initialize() {
map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
geoMarker = new google.maps.Marker();
geoMarker.setPosition(map.getCenter());
geoMarker.setMap(map);
var txtArray = $("#location option").each(function() {
console.log($(this).text());
geocodeAddress($(this).text());
});
$("#location").change(function() {
var addr = ($('#location :selected').text());
console.log(addr);
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': addr
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0].geometry.viewport) {
map.fitBounds(results[0].geometry.viewport);
} else if (results[0].geometry.bounds) {
map.fitBounds(results[0].geometry.bounds);
} else {
map.setCenter(results[0].geometry.location);
}
} else {
alert("Error geocoding address: " + address + "status=" + status);
}
});
});
}
google.maps.event.addDomListener(window, "load", initialize);
function geocodeAddress(address) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title: address
});
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);
} else {
alert("Error geocoding address: " + address + "status=" + status);
}
});
}
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}