Showing posts with label Google Map. Show all posts
Showing posts with label Google Map. Show all posts

Sunday 27 December 2015

How Retrieve Latitude and Longitude via Google Maps API Using it's Address?

Hello

Retrieve Latitude and Longitude via Google Maps API Using it's Address

Here Sample code for Retrieve Latitude and Longitude via Google Maps API Using it's Address

check It can be Use full.


<!DOCTYPE html>
<html>
    <head>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
        <script type="text/javascript">
        function initialize() {
        var address = (document.getElementById('my-address'));
        var autocomplete = new google.maps.places.Autocomplete(address);
        autocomplete.setTypes(['geocode']);
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            var place = autocomplete.getPlace();
            if (!place.geometry) {
                return;
            }

        var address = '';
        if (place.address_components) {
            address = [
                (place.address_components[0] && place.address_components[0].short_name || ''),
                (place.address_components[1] && place.address_components[1].short_name || ''),
                (place.address_components[2] && place.address_components[2].short_name || '')
                ].join(' ');
        }
      });
}
function address_lat_lag() {
    geocoder = new google.maps.Geocoder();
    var address = document.getElementById("my-address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {

      alert("Latitude: "+results[0].geometry.location.lat());
      alert("Longitude: "+results[0].geometry.location.lng());
 
  document.getElementById("lat").value = results[0].geometry.location.lat();
   document.getElementById("lag").value = results[0].geometry.location.lng();
      }

      else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
google.maps.event.addDomListener(window, 'load', initialize);

        </script>
    </head>
    <body>
        <input type="text" id="my-address">
        <button id="getCords" onClick="address_lat_lag();">getLat&Long</button>
       
        You can save here as textbox value
        <br/>
        <label>Latitude</label>
        <input type="text" id="lat"  name="lat"/>
         <label>Longitude</label>
        <input type="text" id="lag"  name="lag"/>
    </body>
</html>