-3

I am beginner in JS. I have this object:

var lc = L.control.locate().addTo(map);
console.log(lc);

it's return: https://ibb.co/r7T4wP7

How can I I need 2 variables: long and lat having values from this object (latitude and longtitude). How to get them?

George
  • 2,330
  • 3
  • 15
  • 36
trakis
  • 45
  • 4
  • 2
    Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Yannick K Jun 10 '19 at 20:07

2 Answers2

0

lc['_event']['latitude'] Will get you to the lattitude and

lc['_event']['longitude'] Will get you to the longitude

For future reference, I would suggest you to Search: Javascript objects variables

or try: https://www.w3schools.com/js/js_variables.asp

Bob Hang
  • 61
  • 5
  • it's not working :( I have error TypeError: undefined is not an object (evaluating 'lc['_event']['latitude']') – trakis Jun 11 '19 at 07:39
0

If you are using Leaflet.Locate plugin, then simply refer to its documentation, which mentions that you can use Leaflet built-in location events:

You can leverage the native Leaflet events onlocationfound and onlocationerror to handle when geolocation is successful or produces an error. You can find out more about these events in the Leaflet documentation.

The linked tutorial provides you with a code snippet how to use the location found event to get the coordinates:

function onLocationFound(e) {
    var radius = e.accuracy; // Note: accuracy is already a radius

    L.marker(e.latlng).addTo(map)
        .bindPopup("You are within " + radius + " meters from this point").openPopup();

    L.circle(e.latlng, radius).addTo(map);
}

map.on('locationfound', onLocationFound);
ghybs
  • 47,565
  • 6
  • 74
  • 99