How can I get google maps location names?

偶尔善良 提交于 2019-12-09 17:29:59

问题


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.


回答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';
      }



回答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.



来源:https://stackoverflow.com/questions/12602822/how-can-i-get-google-maps-location-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!