I'm working with OpenLayers 2 and I managed to create a javascript to locate the users and it works fine. I added a marker representing the current location and when the position change, it creates a new marker. The problem is that it also keeps all the other markers. I'd like to find a way to keep only the last one.

enter image description here

Here is my code:

    function startWatch(){

      if (navigator.geolocation)
        watchId = navigator.geolocation.watchPosition(successCallback,
        alert("Your browser can't get your location");

    function stopWatch(){

    function successCallback(position){

      document.getElementById("lat").innerHTML = position.coords.latitude;
      document.getElementById("long").innerHTML = position.coords.longitude;

      latitude = position.coords.latitude;
      longitude = position.coords.longitude;
      var coord = new OpenLayers.LonLat(longitude,latitude).transform(new                               
      map.setCenter(coord, 16);

      Myposition = new OpenLayers.Layer.Markers("Myposition");
      var size = new OpenLayers.Size(15,15);
      var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
      var icon = new OpenLayers.Icon('img/loc_marker.png', size, offset);
      var mylocation = new OpenLayers.Marker((coord),icon);


    function errorCallback(error){
        case error.PERMISSION_DENIED:
          alert("User did not share geolocation data");
        case error.POSITION_UNAVAILABLE:
          alert("Could not detect current position");
  • Are you only tracking a single user at a time? (Since you are using location from the browser) Feb 2, 2016 at 21:28
  • Yes, only one user at a time.
    – Nick T
    Feb 2, 2016 at 22:19

1 Answer 1


One thing you need to do is move the creation of the "Myposition" layer out of the callback and into an initialization function (possibly as part of startWatch() as long as you remove the layer in stopWatch()). OpenLayers might be smart enough not to recreate the layer if you use the same name (I am not sure which it does), but you might also be creating multiple layers.

Once you do that, you have two options inside your callback. You can either clear all markers on the layer before you add your new marker (not going to work if you are tracking multiple users) or you can delete the previous marker before or after adding your new marker.
Clearing the layer is easier. You just need a reference to your initialized marker layer (be careful of scope) and then call layer.clearMarkers(). Removing the last marker is only slightly more difficult. Just have an external scope variable that keeps a reference to the last marker created, say a variable named oldlocation. When you add your new location, you will remove the old marker, e.g. (assuming your layer reference is still Myposition):

var mylocation = new OpenLayers.Marker((coord),icon);
Myposition.removeMarker(oldlocation); //This can go before the addMarker call
oldlocation = mylocation;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.