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

后端 未结 13 1005
孤城傲影
孤城傲影 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条回答
  •  -上瘾入骨i
    2020-12-07 21:43

    The following code will get the first matching address in the UK without the need to modify the address.

      var geocoder = new google.maps.Geocoder();
      geocoder.geocode(
      {
        'address':  address,
        'region':   'uk'
      }, function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
            for (var i=0; i= 0) {
                        if (results[i].address_components[j].short_name == "GB") {
                            return_address = results[i].formatted_address;
                            return_lat = results[i].geometry.location.lat();
                            return_lng = results[i].geometry.location.lng();
                            ...
                            return;
                        }
                    }
                }
            }
        });
    

提交回复
热议问题