Tinus Erasmus
Tinus Erasmus

Reputation: 15

google geocoding address breakdown

I am very new to this and would like to know how to get the full string and not the first character as it is currently doing,

currently using the example of the html + js from https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

function codeLatLng(callback) {
  var input = document.getElementById('latlng').value;
  var latlngStr = input.split(',', 2);
  var lat = parseFloat(latlngStr[0]);
  var lng = parseFloat(latlngStr[1]);
  var latlng = new google.maps.LatLng(lat, lng);



    geocoder.geocode({'latLng': latlng}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {

      if (results[1]) {
        map.setZoom(17);
        marker = new google.maps.Marker({
            position: latlng,
            map: map



        });
       infowindow.setContent(results[0].formatted_address);
        infowindow.open(map, marker);
    document.getElementById('Addressb').value= results[0].formatted_address; //working
    document.getElementById('Strn').value = results[0].formatted_address[0];
    document.getElementById('Strna').value = results[0].formatted_address[2];




      } else {
        alert('No results found');
     }
   } else {
      alert('Geocoder failed due to: ' + status);
    }
  });

}



google.maps.event.addDomListener(window, 'load', initialize);



    </script>

the results needs to get stored in the specified text fields as bellow

Street Number: <input id="Strn" type="text"><br>
    Street Name: <input id="Strna" type="text"><br>
    Suburb: <input id="Subu" type="text"><br>
    Town: <input id="Town" type="text"><br>
    Code: <input id="Code1" type="text"><br>

Upvotes: 0

Views: 565

Answers (2)

Chris Rosete
Chris Rosete

Reputation: 1258

You have to iterate through the address components and then the types, here is the code

  var streetNumber;
  var streetName;
  var city;
  var state;
  var zip;
  var country;
  for(var i = 0; i < results[0].address_components.length; i++){
    for(var k = 0; k < results[0].address_components[i].types.length; k++){
        if (results[0].address_components[i].types[k] == "street_number")
            streetNumber = results[0].address_components[i].long_name;
        else if (results[0].address_components[i].types[k] == "route")
            streetName = results[0].address_components[i].short_name;
        else if (results[0].address_components[i].types[k] == "locality")
            city = results[0].address_components[i].long_name;
        else if (results[0].address_components[i].types[k] == "administrative_area_level_1")
            state = results[0].address_components[i].long_name;
        else if (results[0].address_components[i].types[k] == "postal_code")
            zip = results[0].address_components[i].long_name;
        else if (results[0].address_components[i].types[k] == "country")
            country = results[0].address_components[i].long_name;
    }
  }

Upvotes: 2

pcrglennon
pcrglennon

Reputation: 457

Here's a link the Google Maps Geocoding docs, specifying the format of the response.

It looks like it does return the components of the address separately, in the address_components array, in a specific order. So if you're geocoding a street address, address_components[0].long_name should return the street address, address_components[1].long_name the street name, etc.

Hope that helps.

Upvotes: 0

Related Questions