Skip to content

Instantly share code, notes, and snippets.

@pradeeprjth
Created August 5, 2021 06:44
Show Gist options
  • Save pradeeprjth/b4cc691c69dc4d97633bf7d311254cfe to your computer and use it in GitHub Desktop.
Save pradeeprjth/b4cc691c69dc4d97633bf7d311254cfe to your computer and use it in GitHub Desktop.
// getAddress method will get the current let long using feolocation that is provided by HTML
function getAddress() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
// here we are setting the value of two hidden input fields with the current let long;
function showPosition(position) {
var let = position.coords.latitude;
var long = position.coords.longitude;
$("#let").val(let);
$("#long").val(long);
$("#gettingtheletlong").val(let+','+long);
// console.log('aa gya lat')
// console.log(lat+lon);
}
// This function will be called by our script that we initialized earlier
function initMap() {
const geocoder = new google.maps.Geocoder();
document.getElementById("getAddress").addEventListener("click", () => {
geocodeLatLng(geocoder);
});
}
// The avobe function will call this function get the current let long from those hidden input
function geocodeLatLng(geocoder) {
const input = document.getElementById("gettingthelatlon").value;
// geting the current let long,
const latlngStr = input.split(",", 2);
const latlng = {
lat: parseFloat(latlngStr[0]),
lng: parseFloat(latlngStr[1]),
};
// spliting and formating the let long value to provide it to geocoder
geocoder.geocode({ location: latlng }, (results, status) => {
if (status === "OK") {
if (results[0]) {
var address = (results[0].formatted_address);
// you will get your current city name in this address variable
// Log the result and you'll find more properties provide by geocode like long_name, short_name, postal code, area building number etc etc
document.getElementById("address1").value = address;
} else {
window.alert("No results found");
}
} else {
window.alert("Geocoder failed due to: " + status);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment