我有我的标记的坐标。现在我想获取标记的地址。 所以我在网上搜索并发现谷歌地图保留地理编码。 现在我尝试执行以下操作:
$.getJSON('http://maps.google.com/maps/api/geocode/json?latlng='+point.lat()+','+ point.lng() +'&key='+apiKey+'&sensor=false&output=json&callback=?',
function(data) {
console.log(data);
});
当我尝试显示地址(即获取 json)时,firebug 会抛出以下错误:
invalid label
开
"status": "OK",\n
我进行了很多搜索,但没有找到解决我问题的答案。 你能告诉我我的代码有什么问题吗?
还有其他方法获取坐标的地址数据吗?
最佳答案
您可能会被same origin policy阻止。昨天我回复了a similar question关于这个主题,您可能想查看一下。
您正在尝试使用 Server-side Geocoding Web Service当 Google map 提供全功能时 Client-side Geocoding API对于 JavaScript。还有一个v2 version ,但最近已弃用。
这会自动解决您的同源问题,此外,请求限制将按客户端 IP 地址计算,而不是按服务器 IP 地址计算,这对于受欢迎的网站来说可能会产生巨大的差异。
客户端服务应该非常易于使用。下面是一个使用 v3 API 的非常简单的反向地理编码示例:
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
var point = new google.maps.LatLng(51.50, -0.12);
if (geocoder) {
geocoder.geocode({ 'latLng': point }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
console.log(results[1].formatted_address);
}
else {
console.log('No results found');
}
}
else {
console.log('Geocoder failed due to: ' + status);
}
});
}
</script>
上面的示例应将 Lambeth, Greater London SE1 7, UK
打印到控制台。
关于javascript - Google map - 保留地理编码 -> 错误 "invalid label",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2927930/