[OpenLayers-Users] Re: help with removing/replacing a markerafter click event

Nicholas Efremov-Kendall n.e.kendall at gmail.com
Wed Jun 15 17:07:34 EDT 2011


Hi Arnd,

Thanks for the suggestions and the examples. I've been trying to implement
the markers on Layer.Vector adapting the code from the example you provided.
I haven't gotten to the point of adding in the conditional statement yet,
but I keep getting and error in the show map function at the var = feat
line, saying that OpenLayers isn't defined. Jslint tells me this is because
OpenLayers hasn't been defined yet. Now, I know I'm noob when it comes to
syntax, but I'm pretty sure that OL is defined earlier. Do you know what's
going on here?

var feat;
var markers = new OpenLayers.Layer.Vector("Markers", {styleMap: new
OpenLayers.StyleMap({externalGraphic:
"/OpenLayers-2.11-rc1/img/marker.png"})});
var map = new OpenLayers.Map('map');
function initGIS(){
map.addControl(new OpenLayers.Control.LayerSwitcher());
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik);
map.setCenter(new OpenLayers.LonLat(-90, 38).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);
var feat = new
Openlayers.LonLat(position.coords.longitude,position.coords.latitude).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());
markers.addFeatures([newFeature(feat)]);
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);
}
function newFeature(feat){
var ptn = new OpenLayers.Geometry.Point(feat);
var ptnFeat = new OpenLayers.Feature.Vector(ptn,null,null);
return(ptnFeat);
}



On Mon, Jun 13, 2011 at 2:13 AM, Arnd Wippermann <arnd.wippermann at web.de>wrote:

>  if you use Feature.Vector markers to display your markers, try
>
> if(vectorLayer.features.length>0)
>     vectorLayer.removeFeatures(vectorLayer.features);
>
>  to remove the marker before you add a marker at the new position.
>
> http://gis.ibbeck.de/ginfo/apps/OLExamples/OL210/Map_mit_Marker.asp
>
> or use a DragFeature.Control to drag the marker around.
>
> http://gis.ibbeck.de/ginfo/apps/OLExamples/OL26/examples/gc_example.html
>
>  Or both.
>
> i was not able to update the position of the marker by updating the
> geometry and redraw the feature. The label moves to the new position, but
> the marker (the externalGraphic) wouldn't move!?
>
> Arnd
>
>
>
>
>
>  ------------------------------
> *Von:* openlayers-users-bounces at lists.osgeo.org [mailto:
> openlayers-users-bounces at lists.osgeo.org] *Im Auftrag von *Nicholas
> Efremov-Kendall
> *Gesendet:* Samstag, 11. Juni 2011 18:43
> *An:* Rossko
> *Cc:* users at openlayers.org
> *Betreff:* Re: [OpenLayers-Users] Re: help with removing/replacing a
> markerafter click event
>
> Hi Rosco,
>
> No, I hadn't thought of that. Any suggestions on how to update the position
> of the marker, maybe a loop with a new LonLat?
>
> On Sat, Jun 11, 2011 at 5:00 AM, Rossko <rossko at culzean.clara.co.uk>wrote:
>
>> > 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
>>
>> Have you considered a different approach to coding this, create one marker
>> and move it around.
>>
>> --
>> View this message in context:
>> http://osgeo-org.1803224.n2.nabble.com/help-with-removing-replacing-a-marker-after-click-event-tp6463400p6464936.html
>> Sent from the OpenLayers Users mailing list archive at Nabble.com.
>> _______________________________________________
>> Users mailing list
>> Users at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20110615/14b5146b/attachment-0001.html


More information about the Users mailing list