geocoder = new google.maps.Geocoder();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng)
}
function errorFunction() {
y.innerHTML = ("Geocoder failed");
}
var city = document.getElementById("location_city");
var latitude = document.getElementById("location_lat");
var longitude = document.getElementById("location_lng");
function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({
'latLng': latlng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//formatted address
city.innerHTML = '<input type="text" name="city" value="' + (results[1].address_components[1].long_name) + ', ' + (results[1].address_components[2].short_name) + ', ' + (results[1].address_components[4].long_name) + '">'
latitude.innerHTML = '<input type="text" name="city" value="' + lat + '">'
longitude.innerHTML = '<input type="text" name="city" value="' + lng + '">'
} else {
y.innerHTML = ("No results found");
}
} else {
y.innerHTML = ("Geocoder failed due to: " + status);
}
});
}
I tried everything but I can not . How can I call this function using <button onclick = "try ( ) " value = "try ( ) " >