map.locate {watch: true} printed location

1,377 views
Skip to first unread message

Miguel Morillo Iruela

unread,
Feb 24, 2013, 11:00:03 AM2/24/13
to leafl...@googlegroups.com
Hello,

I have set to locate my position in the map the next sentence:

map.locate({setView: true, maxZoom: 16, watch: true, enableHighAccuracy: true, maximumAge: 15000, timeout: 3000000,});

It works ok, but every time that my position is refreshed, it is printed a new point. My goal is that there is only exits a printed point with my location every refreshing.

Thanks
Miguel

Ricardo Freitas

unread,
Feb 25, 2013, 7:28:41 AM2/25/13
to leaflet-js
Have you tried defining a variable that would store a value like "position marked  = true" ?

if this variable would be true, then that expression wouldn't be executed. 



Miguel

--
 
---
You received this message because you are subscribed to the Google Groups "Leaflet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-js+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ricardo Freitas

unread,
Feb 25, 2013, 7:29:45 AM2/25/13
to leaflet-js
Maybe there is a method within leaflet that you can configure to just set a new location if no location has been define - my solution is more a "generalistic" solution

Miguel Morillo Iruela

unread,
Feb 25, 2013, 9:08:15 AM2/25/13
to leafl...@googlegroups.com

Thanks Ricardo,

Maybe a function deleting the previous marker? If somebody did something like that, or any help would be appreciated :)

Paulo Vieira

unread,
Feb 25, 2013, 10:11:09 AM2/25/13
to leafl...@googlegroups.com
On Sun, Feb 24, 2013 at 4:00 PM, Miguel Morillo Iruela <miguel....@gmail.com> wrote:

It works ok, but every time that my position is refreshed, it is printed a new point. My goal is that there is only exits a printed point with my location every refreshing.

Miguel, the description of your problem is not clear.

Make a simple and short jsfiddle showing the problem you are facing. That will make it easier to obtain help.

Ricardo Freitas

unread,
Feb 25, 2013, 11:10:28 AM2/25/13
to leaflet-js
Miguel, tell me if this is what you want

A point with your current location is added. When you changed location, a new marker is added. 

You want to remove the previous marker when a new one is added, is that it?



Miguel Morillo Iruela

unread,
Feb 25, 2013, 11:23:43 AM2/25/13
to leafl...@googlegroups.com

Yes.

Miguel Morillo Iruela

unread,
Feb 26, 2013, 4:31:36 AM2/26/13
to leafl...@googlegroups.com

This is what happens when I set the option watch: true, the code:


map.locate({setView: true, maxZoom: 16, watch: true, enableHighAccuracy: true, maximumAge: 15000, timeout: 3000000,});

    map.on('locationfound', onLocationFound);
    map.on('locationerror', onLocationError);

    function onLocationFound(e) {

        var radius = e.accuracy / 2;
        punto = L.circleMarker(e.latlng, {color: '#136AEC', fillColor: '#2A93EE',fillOpacity: 0.7, weight: 2,opacity: 0.9, radius: 4}).addTo(map);
        L.circle(e.latlng,radius , {color: '#136AEC', fillColor: '#136AEC',fillOpacity: 0.05,  weight: 2, opacity: 0.5,radius: radius*2 }).addTo(map);
    }

    function onLocationError(e) {
        alert(e.message);

Ricardo Freitas

unread,
Feb 26, 2013, 4:59:23 AM2/26/13
to leaflet-js
Are you deleting a marker elsewhere on the code? If not, that is your issue: you are not deleting any marker and thus, it keeps adding them to the map.

Tim Taplin

unread,
Mar 27, 2013, 12:17:36 PM3/27/13
to leafl...@googlegroups.com
You should be creating the circleMarker and adding it to your map outside your onLocationFound function and then use onLocationFound to update the location with setLatLng method.

Alternatively, you could do a check to see if the marker exists before creating it, and if it already exists just update the lat lng.

The way you are doing it at the moment, you are creating a new circleMarker each time you call onLocationFound.

Tim Taplin
Reply all
Reply to author
Forward
0 new messages