3

I wrote this function to obtain address from coordinates using Google Maps API.

var geocoder = new google.maps.Geocoder();

function setMap(pos) {
    map.setCenter(pos);

    geocoder.geocode({ latLng: pos }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            $("#address").val(results[0].formatted_address + ", Coordinates: " + results[0].geometry.location.lat() + "," + results[0].geometry.location.lng());

            console.log("init  address", results[0]);
        }
        else {
            $("#address").val("Couldn't find your address, please use search feature.");
        }
    });
}

Then I'm trying to fetch individual address fields from address_components array:

        $.each(address.address_components, function(i, component) {
            $.each(component.types, function(j,type) {
                if (type === 'route') {
                    $("#street_name").val(component.long_name)
                }
                if (type === 'street_number') {
                     $("#street_number").val(component.long_name)
                }
            })
        })

The problem is that for some places formatted_address property conatins street number and street name but it does not contain those as individual fields in address_components.

Example below:

enter image description here

Is it that Google hasn't updated these?

Artur Kedzior
  • 3,994
  • 1
  • 36
  • 58
  • 2
    It might be a data issue on Google side. If the address is modeled incorrectly and operator put street and number as a string value instead of the corresponding address components you will see this behavior. However, to confirm this suspicion we need ask somebody from Google team. – xomena Sep 18 '18 at 23:03
  • In some locations in Oman (in the middle of the desert) the only thing that ```address_components``` contains is plus code and there is no country or any thing else. That means you can not understand where this location comes from – Ali Samie Oct 18 '22 at 05:30

0 Answers0