Get current location LatLong
by aybalasubramanian
HTML
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<p id="Result"></p>
<p id="Spot"></p>
JavaScript
$(document).ready(function () {
GetInfo();
});
function GetInfo() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(ShowPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function ShowPosition(position) {
var latlon = position.coords.latitude + "," + position.coords.longitude;
var x = "<b>Latitude: </b>" + position.coords.latitude +
"<br><b>Longitude: </b>" + position.coords.longitude;
document.getElementById("Result").innerHTML = x;
var img_url = "http://maps.googleapis.com/maps/api/staticmap?center=" + latlon + "&zoom=14&size=400x300&sensor=false";
codeLatLng(position.coords.latitude,position.coords.longitude);
}
function codeLatLng(lat, lng) {
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$("#Spot").text("You are in "+results[0].address_components[3].long_name +", "+results[0].address_components[5].short_name);
} else {
$("#Spot").text("No results found");
}
} else {
$("#Spot").text("Geocoder failed due to: " + status);
}
});
}