If you have projects web application and need to get geo location from user location. You can using javascript for this feature, but.. if get geo location using from browser may be not 100% accuracy, so you can try it.

In your HTML code, describe how get or put data into the markup element id or class.
<form id="contact" action="" method="post" align="center">
<fieldset>
  <input placeholder="Your Address" id="address" type="text" tabindex="1" required autofocus>
</fieldset>
<fieldset>
  <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
</fieldset>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> 

The you can write with this code.
$(document).ready(function ()
{
var currgeocoder;
//Set geo location lat and long
navigator.geolocation.getCurrentPosition(function (position, html5Error) {
    geo_loc = processGeolocationResult(position);
    currLatLong = geo_loc.split(",");
    initializeCurrent(currLatLong[0], currLatLong[1]);
});
//Get geo location result
function processGeolocationResult(position) {
    html5Lat = position.coords.latitude; //Get latitude
    html5Lon = position.coords.longitude; //Get longitude
    html5TimeStamp = position.timestamp; //Get timestamp
    html5Accuracy = position.coords.accuracy; //Get accuracy in meters
    return (html5Lat).toFixed(8) + ", " + (html5Lon).toFixed(8);
}
//Check value is present or
function initializeCurrent(latcurr, longcurr) {
    currgeocoder = new google.maps.Geocoder();
    console.log(latcurr + "-- ######## --" + longcurr);
    if (latcurr != '' && longcurr != '') {
        //call google api function
        var myLatlng = new google.maps.LatLng(latcurr, longcurr);
        return getCurrentAddress(myLatlng);
    }
}
//Get current address
function getCurrentAddress(location) {
    currgeocoder.geocode({
        'location': location
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results[0]);
            $("#address").val(results[0].formatted_address);
        } else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
}
});

Or you can using this code for get latitude and longitude on change from element

<script language="javascript">
var x = document.getElementById("#geo"); function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser. Please enable permision from your devices browser";
    }
}
function showPosition(position) { document.getElementById('latitude').value=position.coords.latitude;
document.getElementById('longitude').value=position.coords.longitude;    
}
</script>

If button Submit on click, addresses from geo location automaticaly put on text, addresed get from Google Maps API

This code has get from jsfiddle.net and for solution on stackoverflow.com (Just posting for my notes only)

FYI: now all applications using maps API required with key identified Google Console.

Post a Comment

 
Top