当用户在地图上拖动标记时,我无法获取该位置。如果用户点击地图并放置标记但是如果它被拖动则代码工作正常。我不太清楚为什么它不起作用?任何帮助都会受到谴责。继承我的代码
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);
}
答案 0 :(得分:0)
“dragend”事件比人们预期的更频繁。我建议你限制这些请求。
或者可以添加一个检查以查看用户是否已完成拖动,然后向反向地理编码器发出请求。