Google Maps Api Get Current Latitude Longitude




To get the current latitude and longitude using the Google Maps API, you can make use of the Geolocation API provided by modern browsers. This API allows you to retrieve the user’s current location.

Here’s an example:

function getCurrentLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    console.log('Geolocation is not supported by this browser.');
  }
}

function showPosition(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;
  
  console.log('Latitude: ' + latitude);
  console.log('Longitude: ' + longitude);
}

In the above example, the getCurrentLocation function is used to fetch the current position of the user. It checks if the browser supports geolocation and then calls the getCurrentPosition method to get the position. The retrieved latitude and longitude values are then displayed in the console using the showPosition function.

Remember to request permission from the user to access their location before calling the getCurrentLocation function.


Leave a comment