使用google api从位置提取省份 [英] extract province from location using google api
问题描述
我正在尝试使用google map api从某个位置提取一些额外信息。我看看它是可能的,但我不明白我在哪里放置bounds参数。这是我的要求:
I'm trying to extract some extra information from a location using google map api. I take a look and it's possible but I don't understand well where I've to put the bounds parameter. This is my request:
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert(results[0].formatted_address);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
我想在地址之后添加绑定:地址但不清楚如何。我试过了,但它不起作用:
I suppose to add the bound after 'address':address but not clear how. I tryied with that but it doesn't works:
geocoder.geocode( { 'address': address, "types":["sublocality","political"]}, function(results, status) {
我不喜欢知道是否熟悉意大利省但我需要这样的东西:
I don't know if have familiar with Italian province but I need something like that:
请求:Venezia
回应:VE或威尼斯
Request: Venezia
Response: VE or Venezia
请求:Murano
回复:VE或Venezia
Request: Murano
Response: VE or Venezia
----改进:
使用 http://maps.googleapis.com/maps/api/geocode/json?address=murano&sensor=false 我可以获得:
{
"results" : [
{
"address_components" : [
{
"long_name" : "Murano",
"short_name" : "Murano",
"types" : [ "natural_feature" ]
},
{
"long_name" : "Venezia",
"short_name" : "Venezia",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Venezia",
"short_name" : "VE",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Veneto",
"short_name" : "Veneto",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Italia",
"short_name" : "IT",
"types" : [ "country", "political" ]
},
{
"long_name" : "30141",
"short_name" : "30141",
"types" : [ "postal_code" ]
}
],
尝试使用alert打印(results [0] .address_components);它没有给我任何东西,我怎么能提取这个:
Trying to print using alert(results[0].address_components); it doesn't show me anything, how can I extract this:
"long_name" : "Venezia",
"short_name" : "VE",
"types" : [ "administrative_area_level_2", "political" ]
谢谢,
Andrea
thanks, Andrea
推荐答案
除了<$ c $迭代,我没有看到任何其他解决方案c> address_components 并将第一个数据与 short_name
匹配,其中包含2个字母。
I do not see any other solution than iterate by address_components
and match first data with short_name
that contains 2 letters.
var len = results[0].address_components.length;
var data;
for ( var i = 0; i < len; i++ ) {
if ( results[0].address_components[i].short_name.length == 2 ) {
data = results[0].address_components[i];
break;
}
}
这篇关于使用google api从位置提取省份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!