Thursday, 24 September 2015

js , angularjs, convert latitude and longitude to address



Example:   http://www.latlong.net/Show-Latitude-Longitude.html



Solution:

function getStreetInfoFromLatlng(value) {
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(value.lat, value.lng);

    value.datas = [];
    geocoder.geocode({
        'latLng': latlng
    }, function(results, status) {
        if (status === google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                //$('#address').val(results[0].formatted_address);
            } else {
                alert('No results found');
            }
        } else {
            alert('Geocoder failed due to: ' + status);
        }
    });
}

you need import this:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=onGoogleReady"></script>






With angularjs

you can call above code from angularjs directly.

http://blog.webbb.be/angular-js-directive/



Note:

1. Some people use "results[1]", but i like "results[0]" which gives you more information.
2. the result should in customer's language (pc or bowser default language.)



google api (not for js api)

https://developers.google.com/maps/documentation/geocoding/intro

No comments:

Post a comment