How to move a marker in an OpenLayers.Layer.Markers layer?

后端 未结 3 939
醉话见心
醉话见心 2021-01-20 21:28

How can I programmatically move an existing marker on an OpenLayers.Layer.Markers layer? I can\'t seem to find a proper way.

Is this supported at all? O

3条回答
  •  独厮守ぢ
    2021-01-20 22:20

    This is not a fully working example demonstrating moving marker on click event

    coffeescript:

    projection_4326 = new OpenLayers.Projection("EPSG:4326") #Transform from WGS 1984
    projection_900913 = new OpenLayers.Projection("EPSG:900913") # Spherical Mercator Projection
    markers = new OpenLayers.Layer.Markers( "Markers")
    marker = new OpenLayers.Marker(new OpenLayers.LonLat(0,0).transform(projection_4326, projection_900913), icon)
    markers.addMarker(marker)
    map = new OpenLayers.Map(...init...stuff)
    map.addLayers([markers])
    map.events.register "click", map, (e) ->
                opx = map.getLayerPxFromViewPortPx(e.xy)
                lonLat = map.getLonLatFromPixel(e.xy)
                # now, if your coordinates are in EPSG:4326 you would have to convert the lonLat here
                #new_position = marker.lonlat.transform(projection_4326, projection_900913)
                marker.map = map
                marker.moveTo(opx) #or new_position
    

提交回复
热议问题