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.
Here is my code:
function startWatch(){
if (navigator.geolocation)
watchId = navigator.geolocation.watchPosition(successCallback,
errorCallback,
{enableHighAccuracy:true,
timeout:Infinity,
maximumAge:0});
else
alert("Your browser can't get your location");
}
function stopWatch(){
navigator.geolocation.clearWatch(watchId);
Emplacement.clearMarkers();
NearestHospital.setVisibility(false);
}
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
OpenLayers.Projection("EPSG:4326"),("EPSG:900913"));
map.setCenter(coord, 16);
Myposition = new OpenLayers.Layer.Markers("Myposition");
map.addLayer(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);
Myposition.addMarker(mylocation)
}
function errorCallback(error){
switch(error.code){
case error.PERMISSION_DENIED:
alert("User did not share geolocation data");
break;
case error.POSITION_UNAVAILABLE:
alert("Could not detect current position");
break;
}
}