当用户在地图上拖动标记时,我无法获取位置。如果用户单击地图并放置标记,则该代码可以正常工作,但如果它被拖动则不能。我不太确定为什么它不起作用?任何帮助将不胜感激。这是我的代码
function initialize() {
var latLng = new google.maps.LatLng(53.34235267846957, -6.264953253906242);
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 7,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
marker = new google.maps.Marker({
map: map,
draggable: true
});
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
updateMarkerPosition(marker.getPosition());
geocodePosition(event.latLng);
});
google.maps.event.addListener(marker, 'position_changed', function() {
updateMarkerPosition(marker.getPosition());
geocodePosition(latLng);
});
google.maps.event.addListener(marker, 'dragend', function() {
geocodePosition(marker.getPosition());
});
}
function placeMarker(location) {
if ( marker ) {
marker.setPosition(location);
map.setCenter(location);
} else {
marker = new google.maps.Marker({
position: location,
map: center
});
}
}
var geocoder = new google.maps.Geocoder();
var geoTimer = setInterval(geocoder, 200);
var geoIndex = 0;
function geocodePosition(pos) {
if (geoIndex < 10) {
geoIndex++;
geocoder.geocode({ latLng: pos }, function(responses, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (responses && responses.length > 0) {
updateMarkerAddress(responses[0].formatted_address);
} else {
updateMarkerAddress('Cannot determine address at this location.');
}
}else{ alert('Cannot determine address at this location.'+status)}
});
}
else {
clearInterval(geoTimer);
}
}
function updateMarkerPosition(latLng) {
document.getElementById('info').innerHTML = [
latLng.lat(),
latLng.lng()
].join(', ');
}
function updateMarkerAddress(str) {
document.getElementById('matchAdd').style.display="block";
document.getElementById('address1').value = str;
document.getElementById('address').innerHTML = str;
}
// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
}