Google's Geocoder returns wrong country, ignoring the region hint

后端 未结 13 955
孤城傲影
孤城傲影 2020-12-07 21:03

I\'m using Google\'s Geocoder to find lat lng coordinates for a given address.

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode(
    {
            


        
相关标签:
13条回答
  • 2020-12-07 21:24

    I find that for a lot of ambiguous queries, the US always takes precedence in Google, even if you try to tell it where to look. You can look at the response and perhaps ignore it if output country=US?

    That is the main reason I stopped using Google Geocoder a while ago and started building my own two years ago.

    https://geocode.xyz/Boston,%20UK will always return the UK location. You can make extra sure by adding region=UK: https://geocode.xyz/Boston,%20UK?region=UK

    0 讨论(0)
  • 2020-12-07 21:25

    I have Created Border All Around Uk and check if my Lat and Lng are in the Range. For 3 k addresses I have around 10 up to 20 addresses in US. I just Ignore them(I can do that in my case) I use lat and lng for creating multi markers on static map with auto zoom. I will share my Solution maybe this will be help for someone. Also I'm Happy to hear different Solutions for my case.

        private static string ReturnLatandLng(string GeocodeApiKey, string address)
        {
            string latlng = "";
    
            Geocoder geocoder = new Geocoder(GeocodeApiKey);
    
            var locations = geocoder.Geocode(address);
    
            foreach (var item in locations)
            {
    
                double longitude = item.LatLng.Longitude;
                double latitude = item.LatLng.Latitude;
                double borderSouthLatitude = 49.895878;
                double borderNorthLatitude = 62.000000;
                double borderWestLongitude = -8.207676;
                double borderEastLongitude = 2.000000;
    
                //Check If Geocoded Address is inside of the UK
                if (( (borderWestLongitude < longitude) && (longitude < borderEastLongitude) ) && ( (borderSouthLatitude < latitude) && (latitude < borderNorthLatitude) ) )
                {
                    latlng = item.LatLng.ToString();
                }
                else
                {
                    latlng = "";
                    Console.WriteLine("GEOCODED ADDRESS IS NOT LOCATED IN UK ADDRESSES LIST. DELETING MARKER FROM MAP.....");
                }
            }
            return latlng;
        }
    
    0 讨论(0)
  • 2020-12-07 21:27

    I prefer a hybrid approach.

    1. Use componentRestrictions to severely limit to country first.
    2. If that doesn't yield enough results, search more broadly (and re-introduce bias if necessary)

      function MyGeocoder(address,region)
      {
          geocoder = new google.maps.Geocoder();
          geocoder.geocode({ 'address': address, 'componentRestrictions': { 'country': region } }, function (r, s) {
              if (r.length < 10) geocoder.geocode({ 'address': address /* could also bias here */ }, function (r2, s2) {
                  for (var j = 0; j < r2.length; j++) r.push(r2[j]);
                  DoSomethingWithResults(r);
              });
              else DoSomethingWithResults(r);
          });
      }
      function DoSomethingWithResults(r) { // Remove Duplicates var d = {}; r = r.filter(function (e) { var h = e.formatted_address.valueOf(); var isDup = d[h]; d[h] = true; return !isDup; });

      // Do something with results }

    0 讨论(0)
  • 2020-12-07 21:38

    I found problems with putting ",UK", setting the region to UK and setting bounds. However, doing ALL THREE seems to fix it for me. Here's a snippet:-

    var sw = new google.maps.LatLng(50.064192, -9.711914)
    var ne = new google.maps.LatLng(61.015725, 3.691406)
    var viewport = new google.maps.LatLngBounds(sw, ne);
    
    geocoder.geocode({ 'address': postcode + ', UK', 'region': 'UK', "bounds": viewport }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
    .....etc.....
    
    0 讨论(0)
  • 2020-12-07 21:39

    I always found this to be pretty easy to filter through since the results can vary and the region doesn't seem to work.

    response( $.map( results, function( item ) {
     if (item.formatted_address.indexOf("GB") != -1) {
        return {
          latitude: item.geometry.location.lat(),
          longitude: item.geometry.location.lng()
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-07 21:42

    Use componentRestrictions attribute:

    geocoder.geocode({'address': request.term, componentRestrictions: {country: 'GB'}}
    
    0 讨论(0)
提交回复
热议问题