Get the full formatted address from Reverse Geocoding

心已入冬 提交于 2019-12-30 05:16:47

问题


I want to retrieve the full formatted address via Google Maps API v3 Reverse Geocoding, since it only shows the postal code and/or city and country in many (maybe all) countries. I want to also retrieve the street name and the other stuff you can get through the Reverse Geocoding (Address Lookup) function. But since I can't get data from XML files or addresses that are linked to an XML file in JavaScript, I must use the Reverse Geocoding function.

Reverse Geocoding (Address Lookup) shows 791 Long Ridge Lane, Gainesboro, Tennessee 38562, USA and Reverse Geocoding shows Gainesboro, Tennessee 38562, USA.

Is it possible to get the full formatted address even through Reverse Geocoding, or do I have to use the PHP XML file through Reverse Geocoding (Address Lookup)?

function coordinates_to_address(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);

    geocoder.geocode({'latLng': latlng}, function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
            if(results[1]) {
                $('#address_current').text(results[1].formatted_address);
            } else {
                alert('No results found');
            }
        } else {
            var error = {
                'ZERO_RESULTS': 'Kunde inte hitta adress'
            }

            // alert('Geocoder failed due to: ' + status);
            $('#address_new').html('<span class="color-red">' + error[status] + '</span>');
        }
    });
}

回答1:


To get the most precise result, use the first result (results[0]), not the second (results[1]):

function coordinates_to_address(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);

    geocoder.geocode({'latLng': latlng}, function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
            if(results[0]) {
                $('#address_current').text(results[0].formatted_address);
            } else {
                alert('No results found');
            }
        } else {
            var error = {
                'ZERO_RESULTS': 'Kunde inte hitta adress'
            }

            // alert('Geocoder failed due to: ' + status);
            $('#address_new').html('<span class="color-red">' + error[status] + '</span>');
        }
    });
}


来源:https://stackoverflow.com/questions/26387713/get-the-full-formatted-address-from-reverse-geocoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!