问题
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".
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:
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
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>
来源:https://stackoverflow.com/questions/43735051/wrong-country-code-returned-by-google-geocoder