Search code examples
javascriptgoogle-mapsgoogle-geocoder

Google Maps Geocoder javascript API with zero results with non english city names


When i try to get a location with google maps Geocoder i am unable to get results with the german language city name. Only if use the english name i get results:

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));
  }
});

However if i try it on the google API Page: https://developers.google.com/maps/documentation/geocoding/intro?hl=de The german language request works fine.

What can i do to get results for the german language request?

Edit:

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

Only the Geocoder does not work..

Solution:

I missed <meta charset="utf-8">


Solution

  • I'm able to get a result for that address. You may want to also look into adding region: 'de' to the geocode request if you are still having issues. Here's an example JSBin adapted from the Google Devs Geocoding Sample

    <!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>