2017-05-29 92 views
0

我找到一種方法可以從google autocomplete或lat和lng獲取正確的城市名稱。現在我用這個代碼:Javascript從google autocomplete或lat和lng獲取城市名稱

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); 
     } 

現在我用一種「地方」的城市名稱,但有時爲其他地址也可能是「administrative_area_level_1」,「administrative_area_level_2」,「administrative_area_level_3」 ...等。 如何從地址獲取正確的城市名稱? 感謝您的幫助。

對於「列寧,水貂,白俄羅斯」我有這樣的結果:

{ 
    "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" 
} 

結果有兩個address_components ...

回答

2

這是我如何提取城市名:(例如工作用地理編碼結果樣品)

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 => 
 
\t \t \t ["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 
 
}

+0

所以你也使用城市的地點類型? 是的,有時它是有效的,但是例如,對於地址「Lenina,Minsk,Belarus」它顯示的城市名稱像「Lenina」,但必須顯示明斯克。 –

+0

你可以添加你的原始問題一個你不能利用的結果的樣本(就像我做的那樣,一個完整的結果JSON)? –

+0

我將json結果添加到了我的文章中。發現結果中有兩個address_components。我怎樣才能只使用firts address_components? –

相關問題