I am new to Stackoverflow, JavaScript and Google Map API. The issue is I am currently getting user current location with below code but how can I return the Latlng value from this and use it in other places in script like in making call for directionsService .
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
//console.log("location--Yes");
} else {
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lng);
const marker = new google.maps.Marker({
position: latlng,
visible: true,
map: map
});
marker.setMap(map);
//console.log("marker should display");
//return latlng;
}
Thanks for help!