Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get google maps location names?

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.

like image 918
dispathis Avatar asked Sep 26 '12 13:09

dispathis


2 Answers

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';
      }
like image 151
kaskader Avatar answered Oct 13 '22 12:10

kaskader


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.

like image 21
geocodezip Avatar answered Oct 13 '22 12:10

geocodezip