2013-05-30 85 views
0

当用户在地图上拖动标记时,我无法获取位置信息。如果用户点击地图并放置标记,但不拖动标记,则代码工作正常。我不知道为什么它不工作?任何帮助都会被忽视。继承人我的代码谷歌地图地理编码器无法正常工作

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

它不起作用?你是否在回复中获得了OVER_QUERY_LIMIT状态? (看起来好像你正在检查状态) – geocodezip

+0

你是什么意思标记状态?它只是返回“无法确定此地点的地址”。 @geocodezip – CoffeeTime

+0

不是“标记状态”,地理编码操作的状态。 [geocoder](https://developers.google.com/maps/documentation/javascript/reference#Geocoder)回调函数签名是geocode(request:GeocoderRequest,callback:function(Array。,GeocoderStatus))' 。你没有看GeocoderStatus。 – geocodezip

回答

0

“dragend”事件的触发方式比人们预期的要多。我建议你扼杀这些要求。

  1. 添加一个标志,以便一次只能向服务器发送一个请求。
  2. 如果自身不(它可能不会)工作,加入

连续请求之间的延迟或者可能添加一个检查,看看用户是否已经完成了拖,然后做请求到反向地理编码器。

+0

再次感谢,我已更新我的代码,尝试解决我在SO上找到的使用setInterval但仍不起作用的解决方案。有什么机会可以帮我解决问题吗? @geocodezip – CoffeeTime