Google maps Google地图地理编码API在传递邮政编码后返回不希望的结果

Google maps Google地图地理编码API在传递邮政编码后返回不希望的结果,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,我使用谷歌地图地理编码API。当我通过邮政编码75116时,我到达了法国巴黎。具体来说:48.8585799,lon=2.284701700000028。我应该去德克萨斯州的邓肯维尔 这是我正在使用的URL: 我尝试将此附加到URL,但没有成功: &组成部分=国家:美国 在maps Javascript API中,必须随geocoderRequest 函数init(){ var map=new google.maps.map(document.getElementById(“地图画布”){

我使用谷歌地图地理编码API。当我通过邮政编码75116时,我到达了法国巴黎。具体来说:48.8585799,lon=2.284701700000028。我应该去德克萨斯州的邓肯维尔

这是我正在使用的URL:

我尝试将此附加到URL,但没有成功:

&组成部分=国家:美国


在maps Javascript API中,必须随
geocoderRequest

函数init(){
var map=new google.maps.map(document.getElementById(“地图画布”){
中心:新google.maps.LatLng(0.0),
缩放:13
}),
geocoder=新的google.maps.geocoder();
地理编码({
地址:75116,
组件限制:{
国家:“美国”
}
},
功能(r,s){
if(s==google.maps.GeocoderStatus.OK){
map.setCenter(r[0].geometry.location);
var marker=new google.maps.marker({
地图:地图,
位置:r[0]。geometry.location
});
}否则{
window.alert('地理编码由于以下原因未成功:'+s);
}
}
);
}
html,
身体,
#地图画布{
身高:100%;
保证金:0;
填充:0;
}

我可以在对“75116”地理编码请求的响应中看到两个位置:巴黎和邓肯维尔。显然,“75116”不是唯一的位置标识符。
请求“US 75116”将导致单个预期位置。

您必须使用地理编码器使用zipcode获取地址:

这里提供了示例代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAtRkkKYEMY8CX1IbWbsAYLqzh-_pecegg&callback=initMap"
        async defer></script>
    <script>

    var map;
    function initMap() {
       var geocoder = new google.maps.Geocoder();
      var myLatlng = new google.maps.LatLng(48.8585799, 2.284701700000028);
       var mapOptions = {
          center : myLatlng,
          zoom : 17
        };
       var map = new google.maps.Map(document.getElementById('map'), mapOptions);
      geocoder.geocode({
            address : '75116',
            componentRestrictions: {
              country: 'us'
            }
        }, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
        } else {
          // alert("no asdsd");
        }
      });

    }
    </script>
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
  </body>
</html>

简单地图
var映射;
函数initMap(){
var geocoder=new google.maps.geocoder();
var mylatng=new google.maps.LatLng(48.8585799,2.284701700000028);
变量映射选项={
中心:myLatlng,
缩放:17
};
var map=new google.maps.map(document.getElementById('map'),mapOptions);
地理编码({
地址:75116,
组件限制:{
国家:“美国”
}
},功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
map.setCenter(结果[0].geometry.location);
}否则{
//警报(“无asdsd”);
}
});
}
html,正文{
身高:100%;
保证金:0;
填充:0;
}
#地图{
身高:100%;
}