I have a vector layer in OL3 that I generate using a .gpx file. I have created another vector layer which includes 2 point geometry features.

I want to be able to click and move the marker features, but constrain their movement so that they can only be moved along the multi point line string of the associated gpx feature vector layer.

I was hoping that the Snap interaction would work, but this messes up the dragging of the map itself.

The code below shows how I am doing this. The 2 marker features are created and added to the same OL source that contains ALL of the features from the GPX file I loaded.

        //create the start and end marker features
    var startMarkerFeature  = _createMarkerFeature(startIconCoords, startIconStyle, "Start Marker");        
    var endMarkerFeature    = _createMarkerFeature(finishIconCoords, finishIconStyle, "End Marker");

    var markerFeatures = new ol.Collection([startMarkerFeature, endMarkerFeature]);

    //Add the start and end marker features to the track point layer containing all of the GPX track features

    //Make the 2 markers draggable
    var markerDragInteraction = new ol.interaction.Modify({features: markerFeatures, style: null});

    //Callback for when user has stopped dragging the marker
    markerDragInteraction.on('modifyend', markerDragged);

    map.addInteraction(new ol.interaction.Snap({source: gpxTrkPointLayer.getSource()}));

This code enables me to drag the 2 markers around, but the drag movement of the 2 features is not locked to the features contained in the same source. I know I am missing something here, but cannot deduce what.

  • It would be nice to see some code. Can you share any, like a live demo of what you're doing? Also, see openlayers.org/en/latest/examples/snap.html. Before being able to drag the markers, they need to be selected in that example. Then, the modify interaciton takes care of dragging points, which prevents the map from being dragged. – Alexandre Dubé Jan 2 '17 at 13:49
  • code added as requested – juckky Jan 2 '17 at 21:40
  • Try setting a higher pixelTolerance in your snap interaction, see: openlayers.org/en/latest/apidoc/ol.interaction.Snap.html – Alexandre Dubé Jan 3 '17 at 12:59
  • I have tried that. Whilst it helps to keep the marker snapped to the features you can still drag the marker away if you wanted too. The bigger issue is that using the pixelTolerance attribute seems to impact the dragging of the map around on the canvas. – juckky Jan 3 '17 at 17:32
  • The other point I forgot to mention is that setting a pixel tolerance also means the feature highlights when you get within the specified distance of it. Starting to wonder whether the Snap interaction is actually the correct or right approach for this problem. – juckky Jan 3 '17 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.