我有一个谷歌地图小工具,工作在一个由Java中的JSF2项目提供服务的.xhtml页面上。地图显示,我有一个功能,用于添加工作得很好的标记。但是,任何使用Geocoder的尝试似乎都会导致返回“ERROR”状态。 Google Maps API在其五个可能的“status”变量返回值的候选者中提到没有此类错误状态。
Followng是一个部署的.js文件,有问题的违规函数是codeAddress()。类似于codeAddress()的代码在我可以找到的其他地方工作,因此它必须与其被调用的环境有关。鉴于地图显示得很好,我可以放置标记,我的期望是我的会话很好。这让我感到困惑:(有谁知道'错误'可能来自什么?
var map;
var marker;
var geocoder;
function placeMarker(location) {
if (!marker) {
marker = new google.maps.Marker({
position: location,
map: map
});
} else {
marker.setPosition(location);
}
map.setCenter(location);
var field = document.getElementById("portCreate:coordinate");
field.value = "POINT (" + location.lat() + " " + location.lng() + ")";
}
function initialize() {
var mapOptions = {
center : new google.maps.LatLng(55.761123, 13.084717),
zoom : 5,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(
document.getElementById("map"), mapOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
setMarker(document.getElementById("portCreate:coordinate"));
geocoder = new google.maps.Geocoder();
}
function setMarker(field) {
if (field.value) {
var regex = /POINT.+?([-0-9\.]+).+?([-0-9\.]+)/;
var matches = regex.exec(field.value);
placeMarker(new google.maps.LatLng(Number(matches[1]), Number(matches[2])));
}
}
function codeAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
} else {
alert("Geocode was not successful for the following reason: " + status + "\nFor address " + address);
}
});
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://maps.googleapis.com/maps/api/js?key=<API key here>&sensor=false&callback=initialize";
document.body.appendChild(script);
}
window.onload = loadScript;
答案 0 :(得分:2)
今天遇到同样的问题之后,我记得我没有链接到API,而是将代码下载为.js。将其更改为链接后,它再次起作用。我认为Google会重新更新脚本,如果它有任何版本冲突,则会返回ERROR状态。
答案 1 :(得分:1)
documentation for javascript/geocoding可能会显示“错误”:
“错误”表示请求超时或联系Google服务器时出现问题。如果您再试一次,请求可能会成功。
答案 2 :(得分:0)
我遇到了同样的问题。我正在使用ASP .Net应用程序。在我将ASP .Net按钮控件更改为HTML输入按钮后,它现在正在工作。不确定问题出在哪里。
<input type="button" value="Geocode" onclick="codeAddress()">
答案 3 :(得分:0)
我正在对类似问题进行排查,我可以通过在返回响应之前取消google API http请求来重新创建错误状态。