Anonmous asked 4 months ago

Requirement in getting current Location

Code to create a custom lwc to get the current lat and long or you can say current Location in the map after clicking a button?

0 0 Answers 1

Answers

Asif Ali answered 3 months ago

Add this code to your js file.

var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};

function success(pos) {
  var crd = pos.coords;

  console.log('Your current position is:');
  console.log(`Latitude : ${crd.latitude}`);
  console.log(`Longitude: ${crd.longitude}`);
  console.log(`More or less ${crd.accuracy} meters.`);
}

function error(err) {
  console.warn(`ERROR(${err.code}): ${err.message}`);
}

navigator.geolocation.getCurrentPosition(success, error, options);
thumb_up 0 thumb_down 0

Login to vote and answer the question.

Get notified of new posts