Nastya Nastya
Nastya Nastya

Reputation: 95

Javascript get city name from google autocomplete or from lat and lng

I find a way to get correct city name from google autocomplete or from lat and lng. Now I use this code:

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

Now I use a type "locality" for city name, but sometimes for other addresses it may be "administrative_area_level_1", "administrative_area_level_2", "administrative_area_level_3" ..etc. How can I get CORRECT city name from address? Thanks for help.

For "Lenina, Minks, Belarus" I have such result:

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

Result has two address_components...

Upvotes: 0

Views: 2992

Answers (1)

Jeremy Thille
Jeremy Thille

Reputation: 26360

Here's how I extract the city name : (Working example with a geocoding results sample)

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
}

Upvotes: 4

Related Questions