Googlemaps Reverse GeoCoding Get Country Name, UK Countries

北战南征 提交于 2019-12-08 07:36:06

问题


I am using google maps Api V3 and using reverse geocoding to find the name of a country clicked on. This appears to work fine for most countries except the countries within the UK.

The UK consists of England, Scotland, Wales and Northern Ireland, which are 4 separate countries, but google maps (and most other country things) see them all as just "UK".

I need to know which particular country within the UK has been clicked and not just that the UK has been clicked!

I am using the following javascript and I am currently just using visual studios "watch" feature to dig down into "results[1]" to see what is contained.

  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(51.8052184317649, -4.965819906250006);
    var myOptions = {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    google.maps.event.addListener(map, 'click', function (event) {
        if (confirm('Is this the location you would like to you for your company?')) {

            geocoder.geocode({ 'latLng': event.latLng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {

                    }
                }
            });
 }
        });
    }

If I click when the map is zoomed quite far out the country name (ie Wales) comes back with types "administrative_area_level_1" and "political" but when you zoom in it's no longer there as it just gives the address which doesn't contain "Wales".

Does anyone know how I get round this? I'm sure other Brits have come across this?


回答1:


Couldn't work out how to reverse geocode to get a UK country as I think it's actually impossible!

Ended up using postcodes and working out countries by that! Not the best, but it works for me!




回答2:


UK countries are represented as administrative_area_level_1 results, look at the 2nd last result in this example (do not rely on address_components for this):

https://maps.googleapis.com/maps/api/geocode/json?&latlng=51.641033,-0.647507



来源:https://stackoverflow.com/questions/5261909/googlemaps-reverse-geocoding-get-country-name-uk-countries

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