[OpenLayers-Users] help with removing/replacing a marker after click event

Nicholas Efremov-Kendall n.e.kendall at gmail.com
Fri Jun 10 14:44:09 EDT 2011


Hi all,

I have a simple application which adds a marker using the geolocation api
after a button click. The issue is that each time the user clicks, another
marker is placed. This isn't really a problem, but for aesthetic purposes,
I'd like to destroy or remove the first marker if the clicks again, rather
than show a series of markers at or close to the user's position because
geolocation based on IP seems to shift it's calculated position, which is I
guess has to do with variations of which servers the users traffic is being
routed through at the time of the call to the navigator.getcurrentposition
call, and would change based on the calculated position if a devices GPS
unit was being utilized. Does anyone have suggestions how to implement this
with the removeMarker() function? I would guess some sort of conditional
where if number of markers = 1, remove marker[0] then addMarker, but I'm not
sure how to get this operational. Thanks in advance. Code is below.

var markers = new OpenLayers.Layer.Markers("Markers");
var map = new OpenLayers.Map('simple_geoloc');
function init_simple_geo(){
map.addControl(new OpenLayers.Control.LayerSwitcher());
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik);
map.setCenter(new OpenLayers.LonLat(-73.98, 40.77).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()),12);
}


jQuery(window).ready(function(){jQuery("#btnInit").click(getLoc);});
function getLoc(){
if (Modernizr.geolocation) {
 navigator.geolocation.getCurrentPosition(showMap,handle_error);
} else {
yqlgeo.get('visitor',norm_yql_resp);
}
}


function showMap(position)
{
var lonlat = new
OpenLayers.LonLat(position.coords.longitude,position.coords.latitude).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());
map.addLayer(markers);
markers.addMarker(new OpenLayers.Marker(lonlat));
var bounds = markers.getDataExtent();
map.zoomToExtent(bounds);
}

function handle_error(error) {
switch(error.code){
case error.PERMISSION_DENIED: alert("This application reguires you to agree
to share your location. Please reload the page to see it work properly.
There is a privacy disclaimer at the bottom of this page. This site does not
currently record or store these data, and in the future will not record
unique user information without informing the user or asking their
permission. -Virtual Spatiality");
break;

case error.POSITION_UNAVAILABLE: alert("could not determine position");
break;

case error.TIMEOUT: alert("timeout retrieving position");
break;

default: alert("unknown error");
break;
}
}

function norm_yql_resp(response){
if (response.error) {
var error = {code : 0};
handle_error(error);
return;
}
var position = {
coords:
{
latitude: response.place.centroid.latitude,
longitude: response.place.centroid.longitude
},
address :
{
city: response.place.locality2.content,
region: response.place.admin1.content,
country: response.place.country.content
}
};
handle_geolocation_query(position);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20110610/9db3f6af/attachment.html


More information about the Users mailing list