Javascript 带有谷歌地理编码 api 的 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4681928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
json with google geocoding api
提问by simplyblue
Here is code which gets latitude and longitute when entered a location.I believe that my code right according to my knowledge.but i get a blank page after entering a place.
这是输入位置时获取纬度和经度的代码。根据我的知识,我相信我的代码是正确的。但是我在输入一个地方后得到一个空白页。
Here is the code:
这是代码:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var url="http://maps.googleapis.com/maps/api/geocode/json?address=";
var query;
var sensor="&sensor=false";
var callback="&callback=?";
$("button").click(function(){
query=$("#query").val();
$.getJSON(url+query+sensor+callback,function(json){
$('#results').append('<p>Latitude : ' + json.results.geometry.location.lat+ '</p>');
$('#results').append('<p>Longitude: ' + json.results.geometry.location.lng+ '</p>');
});
});
});
</script>
</head>
<body>
<input type="text" id="query" /><button>Get Coordinates</button>
<div id="results"></div>
</body>
</html>
回答by J?rgen
In Google Maps Javascript V3 you can access the geocoding service using the google.maps.Geocoder class.
在 Google Maps Javascript V3 中,您可以使用 google.maps.Geocoder 类访问地理编码服务。
var myAddressQuery = 'O. J. Brochs g 16a, Bergen';
var geocoder = new google.maps.Geocoder();
geocoder.geocode(
{ address : myAddressQuery,
region: 'no'
}, function(results, status){
// result contains an array of hits.
});
https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
回答by Ivo Wetzel
You're trying to use JSONP here.
您正在尝试在此处使用 JSONP。
JSONP
If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request > is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.
Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently.
JSONP
如果 URL 包含字符串“callback=?” (或类似的,由服务器端 API 定义),请求 > 被视为 JSONP。有关更多详细信息,请参阅 $.ajax() 中 jsonp 数据类型的讨论。
重要提示:从 jQuery 1.4 开始,如果 JSON 文件包含语法错误,请求通常会以静默方式失败。
See: http://api.jquery.com/jQuery.getJSON/
请参阅:http: //api.jquery.com/jQuery.getJSON/
But the URL you're calling returns plainJSON, so the parsing fails with a syntax error and getJSON
fails silently.
但是您调用的 URL 返回纯JSON,因此解析失败并出现语法错误并且无getJSON
提示地失败。
Now when you try to change the geocode URL to use JSONP, you get a 404
error since Google has removed support for JSONP quite a while ago:
现在,当您尝试更改地理编码 URL 以使用 JSONP 时,您会收到一个404
错误,因为 Google 很久以前就取消了对 JSONP 的支持:
- http://code.google.com/p/gmaps-api-issues/issues/detail?id=1872
- http://blog.mikecouturier.com/2009/11/jsonp-and-google-maps-api-geocoder-not.html
- http://code.google.com/p/gmaps-api-issues/issues/detail?id=1872
- http://blog.mikecouturier.com/2009/11/jsonp-and-google-maps-api-geocoder-not.html
In short:
You cannot simply use the geocode api from Browser JavaScript anymore, you'll have to add a proxy script on your server.
简而言之:
您不能再简单地使用浏览器 JavaScript 中的地理编码 api,您必须在您的服务器上添加一个代理脚本。
And even if the request would work, your code still has a bug in it:
即使请求有效,您的代码中仍然存在错误:
json.results
is an arrayof results, so it has no geometry
property.
json.results
是一个结果数组,所以它没有geometry
属性。
You have to access the first element of the array in order to get to the actual object that has the geometry
property:
您必须访问数组的第一个元素才能访问具有该geometry
属性的实际对象:
json.results[9].geometry.location.lng