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

后端 未结 13 1024
孤城傲影
孤城傲影 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: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 }

提交回复
热议问题