从地址到纬度和经度数字的 Javascript 地理编码不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5984179/
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
Javascript geocoding from address to latitude and longitude numbers not working
提问by Ash
I'm using the following geocoding function to convert a textual address into latitude and longitude numbers, but it's not working right. The alert writes "undefined".
我正在使用以下地理编码函数将文本地址转换为纬度和经度数字,但它无法正常工作。警报写入“未定义”。
Can anyone say what's wrong?
任何人都可以说有什么问题吗?
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.latitude;
var longitude = results[0].geometry.location.longitude;
alert(latitude);
}
});
</script>
回答by Skadlig
Try using this instead:
尝试改用这个:
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
It's bit hard to navigate Google's api but here isthe relevant documentation.
浏览 Google 的 api 有点困难,但这里是相关文档。
One thing I had trouble finding was how to go in the other direction. From coordinates to an address. Here is the code I neded upp using. Please not that I also use jquery.
我很难找到的一件事是如何朝另一个方向前进。从坐标到地址。这是我需要使用的代码。请注意,我也使用 jquery。
$.each(results[0].address_components, function(){
$("#CreateDialog").find('input[name="'+ this.types+'"]').attr('value', this.long_name);
});
What I'm doing is to loop through all the returned address_components
and test if their types match any input element names I have in a form. And if they do I set the value of the element to the address_components
value.
If you're only interrested in the whole formated address then you can follow Google's example
我正在做的是遍历所有返回address_components
并测试它们的类型是否与我在表单中的任何输入元素名称匹配。如果他们这样做,我将元素的address_components
值设置为该值。
如果你只对整个格式化的地址感兴趣,那么你可以按照谷歌的例子
回答by Dancrumb
You're accessing the latitude and longitude incorrectly.
您访问的纬度和经度不正确。
Try
尝试
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude);
}
});
</script>
回答by AllJs
The script tag to the api has changed recently. Use something like this to query the Geocoding API and get the JSON object back
api 的脚本标签最近发生了变化。使用这样的东西来查询地理编码 API 并取回 JSON 对象
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/geocode/json?address=THE_ADDRESS_YOU_WANT_TO_GEOCODE&key=YOUR_API_KEY"></script>
The address could be something like
地址可能类似于
1600+Amphitheatre+Parkway,+Mountain+View,+CA (URI Encoded; you should Google it. Very useful)
1600+Amphitheatre+Parkway,+Mountain+View,+CA(URI 编码;你应该谷歌一下。非常有用)
or simply
或者干脆
1600 Amphitheatre Parkway, Mountain View, CA
1600 Amphitheatre Parkway, 山景城, CA
By entering this address https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
inside the browser, along with my API Key, I get back a JSON object which contains the Latitude & Longitudefor the city of Moutain view, CA.
通过https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
在浏览器中输入这个地址以及我的API Key,我得到一个 JSON 对象,其中包含加州山景城的纬度和经度。
{"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4222556,
"lng" : -122.0838589
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4236045802915,
"lng" : -122.0825099197085
},
"southwest" : {
"lat" : 37.4209066197085,
"lng" : -122.0852078802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}],"status" : "OK"}
Web Frameworks such like AngularJSallow us to perform these queries with ease.
像AngularJS这样的 Web 框架允许我们轻松地执行这些查询。