2

I'm using Google Maps API v3 to handle a map. In addition I took an input/text and an input/button to let the user look for a zip code.

The map then should center on that zipcode. And it does! But on some zip codes I come to very exotic places.

The server, client and anything else is in Germany, so I would like to lock all queries to Germany. For example if I want to center on "92224" I don't see Bavaria, I get lost in Lithuania.

I'm using the following code from this answer to center by command:

function codeAddress(zipCode) {
    geocoder.geocode( { 'address': zipCode}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        //Got result, center the map and put it out there
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }

Now how can I achieve that this query only will center on German zip codes? (If it helps, German zip codes are always 5 numbers long.)

Community
  • 1
  • 1
Trollwut
  • 541
  • 1
  • 7
  • 23

1 Answers1

1

You want to setup componentRestrictions and filter by country.

See the documentation.

Example:

function codeAddress(zipCode) {

    geocoder.geocode({
        'address': address, "componentRestrictions":{"country":"DE"}
    }, function (results, status) {

        if (status == google.maps.GeocoderStatus.OK) {

            map.setCenter(results[0].geometry.location);

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });

        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

Demo:

JSFiddle demo

Note:

As a side note, you cannot filter by multiple countries. This is a long awaited feature that Google still didn't implement until now.

Edit: 2017-04-19

Google has now implemented a way to filter by up to 5 countries: see https://issuetracker.google.com/issues/35821685 I will not make any comment about how much useful this is...

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
  • Aaaaand it works! Perfect, thanks! :) Is there a way to tell Google Maps to center it with a disctance of ~100 km? Because on Lithuania it did, now it just centers. – Trollwut Oct 23 '14 at 14:52
  • I didn't get your question. What do you mean by *center with a distance*? – MrUpsidown Oct 23 '14 at 15:18
  • Sry. Your function centers well to the zip code I enter. But I then see the centered point in a distance what I had before. I would like to also set the distance of view to about 100 km. So that when you search for a zip code, you also see the surrounding scenery and not the whole country. :) – Trollwut Oct 24 '14 at 12:01
  • 1
    Well just set the zoom level to what you want. `map.setZoom(10)` for example. http://jsfiddle.net/upsidown/8e6xh7sc/2/ – MrUpsidown Oct 24 '14 at 12:07
  • Lucky easteregg, I guess: If you enter a zip code which may be valid but is not used in Germany, you're transfered to Mühlhausen. The Duke of Münchhausen is known to be a folklore about lying. (Don't know if Google intended this or if this is serendipity.) – Trollwut Oct 29 '14 at 12:15
  • Well... Nice story, but I don't think Mühlhausen/Münchhausen are related ;). If you check the results, it returns *Germany*. And that point is right in the middle of Germany. Why doesn't it return *ZERO_RESULTS* I can't tell though. – MrUpsidown Oct 29 '14 at 14:38
  • So Mühlhausen isnt even lying when they say theyre taking center... But thanks for that ticking off. :D – Trollwut Oct 30 '14 at 17:36
  • Maybe it doesnt get `ZERO_RESULTS` because of the language filter. So the code knows that it has to look in `Germany` but cant find any further. So it maybe returns the last known superior data. – Trollwut Oct 30 '14 at 17:38
  • You mean the country filter, right? The same *inexistent* zip code returns `ZERO_RESULTS` if no `componentRestrictions` is set on the country. So that sounds like it changes the behavior. – MrUpsidown Oct 31 '14 at 08:41
  • This is not working for me. Even in the fiddle. When i enter a nonexisting (in Germany) ZipCode (e.g 20030) i get centered on USA. Even though the componentRestrictions say country:DE – Toastgeraet Aug 10 '16 at 10:37