extract province from location using google api

I'm trying to extract some extra information from a location using google map api. I take a look and it's possible but I don't understand well where I've to put the bounds parameter. This is my request:

var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        alert(results[0].formatted_address);    
    } else {
        alert("Geocode was not successful for the following reason: " + status);
    }
});

I suppose to add the bound after 'address':address but not clear how. I tryied with that but it doesn't works:

geocoder.geocode( { 'address': address, "types":["sublocality","political"]}, function(results, status) {

I don't know if have familiar with Italian province but I need something like that:

Request: Venezia
Response: VE or Venezia

Request: Murano
Response: VE or Venezia

---- Improved:

Using http://maps.googleapis.com/maps/api/geocode/json?address=murano&sensor=false I can obtain this:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Murano",
               "short_name" : "Murano",
               "types" : [ "natural_feature" ]
            },
            {
               "long_name" : "Venezia",
               "short_name" : "Venezia",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Venezia",
               "short_name" : "VE",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Veneto",
               "short_name" : "Veneto",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Italia",
               "short_name" : "IT",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "30141",
               "short_name" : "30141",
               "types" : [ "postal_code" ]
            }
         ],

Trying to print using alert(results[0].address_components); it doesn't show me anything, how can I extract this:

           "long_name" : "Venezia",
           "short_name" : "VE",
           "types" : [ "administrative_area_level_2", "political" ]

thanks, Andrea

Answers:

Answer

I do not see any other solution than iterate by address_components and match first data with short_name that contains 2 letters.

var len = results[0].address_components.length;
var data;
for ( var i = 0; i < len; i++ ) {
    if ( results[0].address_components[i].short_name.length == 2 ) {
        data = results[0].address_components[i];
        break;
    }
}
Answer

You want to look for "administrative_area_level_2" in the types array and return the object so you will need nested loops:

response= {
            "results" : [{
                "address_components" : [{
                    "long_name" : "Murano",
                    "short_name" : "Murano",
                    "types" : [ "natural_feature" ]
                },{
                    "long_name" : "Venezia",
                    "short_name" : "Venezia",
                    "types" : [ "locality", "political" ]
                },{
                    "long_name" : "Venezia",
                    "short_name" : "VE",
                    "types" : [ "administrative_area_level_2", "political" ]
                },{
                    "long_name" : "Veneto",
                    "short_name" : "Veneto",
                    "types" : [ "administrative_area_level_1", "political" ]
                },{
                    "long_name" : "Italia",
                    "short_name" : "IT",
                    "types" : [ "country", "political" ]
                },{
                    "long_name" : "30141",
                    "short_name" : "30141",
                    "types" : [ "postal_code" ]
                }
                ]
            }
            ]
        }

    for (var i=0; i<response.results[0].address_components.length; i++) {
            for (var b=0;b<response.results[0].address_components[i].types.length;b++) {
                if (response.results[0].address_components[i].types[b] == "administrative_area_level_2") {
                    //this is the object you are looking for
                    province = response.results[0].address_components[i];
                    break;
                }
            }
        }
        alert(province.short_name + " " + province.long_name)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.