Google Geocoder返回的国家/地区代码错误

时间:2017-05-02 10:00:49

标签: google-maps-api-3 google-geocoder reverse-geocoding

使用Google Places API对点位置进行地理编码时,我收到的国家/地区代码不正确。这主要发生在国家边界附近,所以我认为Places API使用的国家边界(精度较低)的源不同于Google Map图块中显示的边界。

在下面的示例中,该点明显位于法国境内,但Google Places返回的ISO代码为“西班牙”的“ES”。

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 个答案:

答案 0 :(得分:0)

您正在使用的反向地理编码结果的位置位于边框的另一侧。

(顺便说一句 - 这是Google Reverse Geocoder,而不是Google Places API)

proof of concept fiddle

下图中的

蓝色标记是反向地理编码结果的位置 blue marker is location of reverse geocoded result

代码段

&#13;
&#13;
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);
&#13;
#map {
  height: 400px;
  width: 100%;
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>
&#13;
&#13;
&#13;