Search code examples
javascriptgoogle-geolocation

Get Lon. Lat. for given postcode within a given country


I'm fairly new to the geolocation api of Google...

I'm using the following URL to retrieve geolocation information for a given address.

http://maps.googleapis.com/maps/api/geocode/json?address=address_value

The address paramter can contain several information. My research on correct formatting of values for the address paramater always led me to using a format like postcode, country but this doesn't seem to always work as desired.

My question is : How can I always get the the information only for a given country (let's say Germany)?

Example:

The Berlin postcode range is 10115-14199

When I try the following http://maps.googleapis.com/maps/api/geocode/json?address=10115,%20Germany

I get the correct information (location in Germany):

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "10115",
               "short_name" : "10115",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Mitte",
               "short_name" : "Mitte",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Berlin",
               "short_name" : "Berlin",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Berlin",
               "short_name" : "Berlin",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Deutschland",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "10115 Berlin, Deutschland",
...

When I then try this http://maps.googleapis.com/maps/api/geocode/json?address=10696,%20Germany (which is an existing postcode in Berlin, Germany as well) i get an address in the USA (where the postcode was interpreted as street number)

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "10696",
               "short_name" : "10696",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Berlin Road",
               "short_name" : "Berlin Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Berlin Heights",
               "short_name" : "Berlin Heights",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Berlin Township",
               "short_name" : "Berlin Township",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Erie County",
               "short_name" : "Erie County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Ohio",
               "short_name" : "OH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "USA",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "44814",
               "short_name" : "44814",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "10696 Berlin Rd, Berlin Heights, OH 44814, USA",

...

Solution

  • You need to use components such as

    http://maps.googleapis.com/maps/api/geocode/json?address=10696&components=country:DE