Search code examples
google-maps-api-3geocodingreverse-geocodingcountries

Googlemaps Reverse GeoCoding Get Country Name, UK Countries


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?


Solution

  • 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!