Get the full formatted address from Reverse Geocoding

后端 未结 1 750
感情败类
感情败类 2021-01-03 05:48

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

相关标签:
1条回答
  • 2021-01-03 06:23

    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>');
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题