Sunday 20 May 2018

Get Latitude and Longitude from address JavaScript

get latitude and longitude from address javascript

Description:
In this example we explain that how to get Latitude and Longitude from Address using JavaScript. Or how to get Latitude and Longitude of the Map using JavaScript. Or how to find Latitude and Longitude based on address enter by user in dynamic CRM using JavaScript.

So here we demonstrate that how to get or find Latitude and Longitude form address using JavaScript. We used Geocoding service to get Latitude and Longitude from address using JavaScript.
Code:

<!-- To use Geocoding from Google Maps V3 you need to link https://maps.googleapis.com/maps/api/js?sensor=false -->
<div>
     <h3> Enter an adress and press the button</h3>

    <input id="address" type="text" placeholder="Enter address here" />
    <button id="btn">Get LatLong</button>
    <div>
        <p>Latitude:
            <input type="text" id="latitude" readonly />
        </p>
        <p>Longitude:
            <input type="text" id="longitude" readonly />
        </p>
    </div>
</div>


function showResult(result) {
            document.getElementById('latitude').value = result.geometry.location.lat();
            document.getElementById('longitude').value = result.geometry.location.lng();
        }

        function getLatitudeLongitude(callback, address) {
            // If adress is not supplied, use default value 'Ferrol, Galicia, Spain'
            address = address || 'Ferrol, Galicia, Spain';
            // Initialize the Geocoder
            geocoder = new google.maps.Geocoder();
            if (geocoder) {
                geocoder.geocode({
                    'address': address
                }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        callback(results[0]);
                    }
                });
            }
        }

        var button = document.getElementById('btn');

  button.addEventListener("click", function () {
            var address = document.getElementById('address').value;
            getLatitudeLongitude(showResult, address)
        });


0 comments:

Post a Comment