使用非英语城市名称的Google Maps Geocoder javascript API结果为零

时间:2018-03-14 09:43:41

标签: javascript google-maps google-geocoder

当我尝试使用谷歌地图Geocoder获取位置时,我无法获得德语城市名称的结果。只有使用英文名称才能获得结果:

var geocoder = new google.maps.Geocoder();

var address = "schildergasse 1, köln, deutschland"; // No Results
// var address = "schildergasse 1, cologne, deutschland"; // GET Results

geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK)
  {
     alert(JSON.stringify(results[0].geometry.location));
  }else{
     alert(JSON.stringify(status));
  }
});

但是如果我在google API页面上试用它:https://developers.google.com/maps/documentation/geocoding/intro?hl=de 德语语言请求正常。

如何获得德语语言请求的结果?

修改

这也有效: http://maps.googleapis.com/maps/api/geocode/json?address=%27schildergasse+1,köln,deutschland%27&sensor=false%27?key=MY_API_KEY

只有地理编码器不起作用..

解决方案:

我错过了<meta charset="utf-8">

1 个答案:

答案 0 :(得分:2)

我能够获得该地址的结果。如果您仍然遇到问题,您可能还需要考虑将region: 'de'添加到地理编码请求中。这是example JSBin改编自Google Devs Geocoding Sample

link
<!DOCTYPE html>
<html>
  <head>
    <title>Geocoding service</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #floating-panel {
        position: absolute;
        top: 10px;
        left: 25%;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
        text-align: center;
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }
    </style>
  </head>
  <body>
    <div id="floating-panel">
      <input id="address" type="textbox" value="schildergasse 1, köln, deutschland">
      <input id="submit" type="button" value="Geocode">
    </div>
    <div id="map"></div>
    <script>
      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 8,
          center: {lat: -34.397, lng: 150.644}
        });
        var geocoder = new google.maps.Geocoder();

        geocodeAddress(geocoder, map);

        document.getElementById('submit').addEventListener('click', function() {
          geocodeAddress(geocoder, map);
        });
      }

      function geocodeAddress(geocoder, resultsMap) {
        var address = document.getElementById('address').value;
        geocoder.geocode({
          address: address
        }, function(results, status) {
          if (status === 'OK') {
            resultsMap.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
              map: resultsMap,
              position: results[0].geometry.location
            });
            var infoWindow = new google.maps.InfoWindow({
                content: results[0].formatted_address
            });
            infoWindow.open(map, marker);
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_KEY">
    </script>
  </body>
</html>