Wrong country code returned by Google Geocoder

2019-09-14 19:31发布

I'm getting incorrect country codes when using Google Places API to geocode point locations. This happens mainly near country borders so I suppose that Places API is using a different source for country borders (with less precision) than the one shown in the Google Map tiles.

In the example below, the point is clearly inside France but the returned ISO code by Google Places is "ES" for "Spain".

enter image description here

function initMap() {
    var geocoder = new google.maps.Geocoder;
    var point = new google.maps.LatLng(42.4241355,2.2915667);
    var map = new google.maps.Map(document.getElementById('map'), {  zoom: 16, center: point });
    var country_name = "Undefined";
    geocoder.geocode({'location': point}, function(results, status, country_name) {
        if (status === google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                for (var i=0; i < results[0].address_components.length; i++) {
                    for (var j=0; j < results[0].address_components[i].types.length; j++) {
                        if (results[0].address_components[i].types[j] == "country") {
		        country = results[0].address_components[i];
			    }
		        }
		    }
		    country_name =  country.short_name;
		} else {
		    country_name = "Geocoder no results found";
		}
	     } else {
	        country_name = 'Geocoder failed due to: ' + status;
	     }
  	     var marker = new google.maps.Marker({ position: point, map: map }); 
  	     var infowindow = new google.maps.InfoWindow({ content: country_name });
	     infowindow.open(map, marker);
	});
}
#map { height: 400px; width: 100%; }
<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyABsURr0TnnANUUgsqN_Rk2VXhqgZfZrEk&callback=initMap"> </script>

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-14 20:02

The location of the reverse geocoded result that you are using is on the other side of the border.

(BTW - this is the Google Reverse Geocoder, not the Google Places API)

proof of concept fiddle

blue marker in image below is location of reverse geocoded result blue marker is location of reverse geocoded result

code snippet:

function initMap() {
  var geocoder = new google.maps.Geocoder;
  var point = new google.maps.LatLng(42.4241355, 2.2915667);
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 16,
    center: point
  });
  var country_name = "Undefined";
  var bounds = new google.maps.LatLngBounds();
  bounds.extend(point);
  geocoder.geocode({
    'location': point
  }, function(results, status, country_name) {
    if (status === google.maps.GeocoderStatus.OK) {
      if (results[0]) {
        for (var i = 0; i < results[0].address_components.length; i++) {
          for (var j = 0; j < results[0].address_components[i].types.length; j++) {
            if (results[0].address_components[i].types[j] == "country") {
              country = results[0].address_components[i];
              console.log("country=" + country.short_name + ":" + country.long_name + " i=" + i + " j=" + j);
              console.log("results=" + JSON.stringify(results));
              var marker = new google.maps.Marker({
                position: results[0].geometry.location,
                map: map,
                icon: "http://maps.google.com/mapfiles/ms/micons/blue.png"
              });
              bounds.extend(results[0].geometry.location);
              map.fitBounds(bounds);
            }
          }
        }
        country_name = country.short_name;
      } else {
        country_name = "Geocoder no results found";
      }
    } else {
      country_name = 'Geocoder failed due to: ' + status;
    }
    var marker = new google.maps.Marker({
      position: point,
      map: map
    });
    var infowindow = new google.maps.InfoWindow({
      content: country_name
    });
    infowindow.open(map, marker);
  });
}
google.maps.event.addDomListener(window, "load", initMap);
#map {
  height: 400px;
  width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>

查看更多
登录 后发表回答