Skip to content

Instantly share code, notes, and snippets.

@asiermarques
Last active August 3, 2016 22:57
Show Gist options
  • Save asiermarques/21431cc61e90d57ac927 to your computer and use it in GitHub Desktop.
Save asiermarques/21431cc61e90d57ac927 to your computer and use it in GitHub Desktop.
openlayers draggable "marker"
var initMap = function(){
var Lon = -2.93 ;
var Lat = 43.2629642;
var Zoom = 14;
var EPSG4326 = new OpenLayers.Projection( "EPSG:4326" );
var EPSG900913 = new OpenLayers.Projection("EPSG:900913");
var LL = new OpenLayers.LonLat( Lon, Lat );
var XY = LL.clone().transform( EPSG4326, EPSG900913 );
map = new OpenLayers.Map('map',{ projection: EPSG900913});
//Open Street Maps layer
map.addLayer(new OpenLayers.Layer.OSM());
map.setCenter(XY, Zoom);
var deftColor = "#00FF00";
var deftIcon = "marker.png";
var featureHeight = 34;
var featureWidth = 20;
var featureStyle = {
fillColor: deftColor,
strokeColor: deftColor,
pointRadius: 1,
externalGraphic:deftIcon,
graphicWidth: featureWidth,
graphicHeight: featureHeight,
graphicXOffset: -featureWidth/2,
graphicYOffset: -featureHeight,
label: "Muéveme...",
fontColor: "#000000",
fontSize: "10px",
fontWeight: "bold",
labelAlign: "rm"
};
var vectorL = new OpenLayers.Layer.Vector( "Vector Layer", {
styleMap: new OpenLayers.StyleMap( featureStyle )
});
map.addLayer( vectorL );
var dragVectorC = new OpenLayers.Control.DragFeature( vectorL, {
onDrag: function(feature, pixel){
//Don´t user the position of the pixel or the feature, use the point position instead!
var point = feature.geometry.components[0];
var llpoint = point.clone()
llpoint.transform( new OpenLayers.Projection(EPSG900913),
new OpenLayers.Projection(EPSG4326));
}});
map.addControl( dragVectorC );
dragVectorC.activate();
var point = new OpenLayers.Geometry.Point( XY.lon, XY.lat );
var featureOb = new OpenLayers.Feature.Vector( new OpenLayers.Geometry.Collection([point]) );
vectorL.addFeatures( [featureOb] );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment