【发布时间】:2012-05-13 20:09:02
【问题描述】:
我正在使用反向地理编码填写四个表单域。表单填写正确,但随后我收到一条错误消息,指出 OVER_QUERY_LIMIT。在检查 chrome 的 JS 控制台时,我看到在收到错误消息之前已经进行了五个相同的调用。我不确定为什么我的代码会发出多个请求。有人可以指出我应该在 codeLatLng 函数的循环中更改什么。这很可能是问题所在。
var geocoder;
function getCity() {
geocoder = new google.maps.Geocoder();
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
//alert("Success funciton" + lat + " " + lng);
codeLatLng(lat, lng)
}
function errorFunction(){
AppMobi.notification.alert("For the best user experience, please ensure your device GPS and geolocation settings are enabled.","Geolocation disabled","OK");
}
function codeLatLng(lat, lng) {
//alert("codeLatLng fn started");
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//formatted address
//alert(results[0].formatted_address) //Complete address
$("#address1").val(results[0].formatted_address);
} else {
alert("No results found. Please enter the data manually.");
}
} else {
alert("Geocoder failed due to: " + status +". Please enter the data manually." );
}
});
非常感谢。
【问题讨论】:
-
这不可能是你的全部代码,因为此时
getCity()没有被调用,这意味着geocoder永远不会被定义并且geocoder.geocode()将会失败。 -
您可能需要提供更多代码、页面链接或设置 jsFiddle 页面。我在
codeLatLng函数中看不到循环。我看不到地理编码器是如何被循环调用的。
标签: javascript google-maps-api-3