Google Geocoder返回的国家/地区代码错误 [英] Wrong country code returned by Google Geocoder

查看:83
本文介绍了Google Geocoder返回的国家/地区代码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google Places API对点位置进行地理编码时,我得到的国家/地区代码不正确.这主要发生在国家/地区边界附近,因此我认为Places API使用的国家/地区边界来源(而不是精确度)与Google Map磁贴中显示的来源不同.

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.

在下面的示例中,该点显然在法国境内,但是Google地方信息返回的ISO代码是西班牙"的"ES".

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>

推荐答案

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

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

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

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

概念提琴证明

蓝色标记是反向地理编码结果的位置

blue marker in image below is location of reverse geocoded result

代码段:

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>

这篇关于Google Geocoder返回的国家/地区代码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆