gpt4 book ai didi

Javascript从谷歌自动完成或从纬度和经度获取城市名称

转载 作者:行者123 更新时间:2023-12-01 03:36:32 25 4
gpt4 key购买 nike

我找到了一种从谷歌自动完成或纬度和经度获取正确城市名称的方法。现在我使用这段代码:

google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
for (var i = 0; i < place.address_components.length; i++) {
for (var j = 0; j < place.address_components[i].types.length; j++) {
if (place.address_components[i].types[j] == "locality") {$("#city").val(place.address_components[i].long_name);
}

现在我使用“locality”类型作为城市名称,但有时对于其他地址,它可能是“administrative_area_level_1”、“administrative_area_level_2”、“administrative_area_level_3”等。如何从地址中获取正确的城市名称?感谢帮助。

对于“Lenina,Minks,白俄罗斯”,我有这样的结果:

 {
"results" : [
{
"address_components" : [
{
"long_name" : "vulica Lienina",
"short_name" : "vulica Lienina",
"types" : [ "route" ]
},
{
"long_name" : "Minsk",
"short_name" : "Minsk",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Belarus",
"short_name" : "BY",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "vulica Lienina, Minsk, Belarus",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 53.9049197,
"lng" : 27.5804421
},
"southwest" : {
"lat" : 53.8868641,
"lng" : 27.553896
}
},
"location" : {
"lat" : 53.8958051,
"lng" : 27.5659861
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 53.9049197,
"lng" : 27.5804421
},
"southwest" : {
"lat" : 53.8868641,
"lng" : 27.553896
}
}
},
"place_id" : "ChIJL_x0NsTP20YRE6LICTM5v2I",
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "Lenina",
"short_name" : "Lenina",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Slucki rajon",
"short_name" : "Slucki rajon",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Minskaja voblasć",
"short_name" : "Minskaja voblasć",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Belarus",
"short_name" : "BY",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Lenina, Belarus",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 53.0604058,
"lng" : 27.2418881
},
"southwest" : {
"lat" : 53.0405422,
"lng" : 27.2215462
}
},
"location" : {
"lat" : 53.0546355,
"lng" : 27.229549
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 53.0604058,
"lng" : 27.2418881
},
"southwest" : {
"lat" : 53.0405422,
"lng" : 27.2215462
}
}
},
"place_id" : "ChIJNTp2Gl6t2UYRI1cE6L4oVh8",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}

结果有两个地址组件...

最佳答案

以下是我提取城市名称的方法:(带有地理编码结果示例的工作示例)

let results = [
{
"address_components" : [
{
"long_name" : "vulica Lienina",
"short_name" : "vulica Lienina",
"types" : [ "route" ]
},
{
"long_name" : "Minsk",
"short_name" : "Minsk",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Belarus",
"short_name" : "BY",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "vulica Lienina, Minsk, Belarus",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 53.9049197,
"lng" : 27.5804421
},
"southwest" : {
"lat" : 53.8868641,
"lng" : 27.553896
}
},
"location" : {
"lat" : 53.8958051,
"lng" : 27.5659861
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 53.9049197,
"lng" : 27.5804421
},
"southwest" : {
"lat" : 53.8868641,
"lng" : 27.553896
}
}
},
"place_id" : "ChIJL_x0NsTP20YRE6LICTM5v2I",
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "Lenina",
"short_name" : "Lenina",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Slucki rajon",
"short_name" : "Slucki rajon",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Minskaja voblasć",
"short_name" : "Minskaja voblasć",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Belarus",
"short_name" : "BY",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Lenina, Belarus",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 53.0604058,
"lng" : 27.2418881
},
"southwest" : {
"lat" : 53.0405422,
"lng" : 27.2215462
}
},
"location" : {
"lat" : 53.0546355,
"lng" : 27.229549
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 53.0604058,
"lng" : 27.2418881
},
"southwest" : {
"lat" : 53.0405422,
"lng" : 27.2215462
}
}
},
"place_id" : "ChIJNTp2Gl6t2UYRI1cE6L4oVh8",
"types" : [ "locality", "political" ]
}
]

let matches = results[0].address_components.filter(address_component =>
["locality", "colloquial_area"].some(word => ~address_component.types.indexOf(word)))

if (!matches || !matches.length) {
console.log("Could not resolve city name.")
} else {
console.log("Found city : ", matches[0].short_name) // Prints : Minsk
}

关于Javascript从谷歌自动完成或从纬度和经度获取城市名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44239461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com