How can I get google maps location names?

11,635

Solution 1

You need to parse the response to get that data out, so for example if you want to get the country and results is the result object you get by calling the reverse Gecoding: https://developers.google.com/maps/documentation/javascript/geocoding#ReverseGeocoding

Then the function for getting the country would be:

       function getCountry(results) {
         var geocoderAddressComponent,addressComponentTypes,address;
         for (var i in results) {
           geocoderAddressComponent = results[i].address_components;
           for (var j in geocoderAddressComponent) {
             address = geocoderAddressComponent[j];
             addressComponentTypes = geocoderAddressComponent[j].types;
             for (var k in addressComponentTypes) {
               if (addressComponentTypes[k] == 'country') {
                 return address;
               }
             }
           }
         }
        return 'Unknown';
      }

Solution 2

If you need the "name" associated with a place on a Google Maps API v3 map based on its geographic coordinates (latitude and longitude), use the reverse geocoder, it returns many levels of information for that location.

Example from the documentation

Note, that except for the fact that it won't necessarily correlate with the Google Maps API v3 tiles, geonames.org might have the information you need or a less restrictive service to get it.

Share:
11,635
Admin
Author by

Admin

Updated on June 20, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm building a site in which it is required to get all google maps locations, from countries names to the smallest village. Is this anywhere in the api? Because it is nowhere to be found.