1

Is it possible to add a popup (without a marker icon) to a Vector (LineString)?

This is my code, if possible, how to change it to add the popup? I need the popup to display some additional information to the road segement.

<script>
var ytrajectory=[];
ytrajectory.push('LINESTRING (11.1119339 46.0772706, 11.1117177 46.077171, 11.111613 46.0770837, 11.1115376 46.07702)');
ytrajectory.push('LINESTRING (11.1115376 46.07702, 11.1114585 46.0768611, 11.1114219 46.0767673)');

...
</script>
function showReconstructedTrajectoryy() {
var vectorLayer = new OpenLayers.Layer.Vector(
"Reconstructed trajectoryy", {
style : reconstructedTrajectoryStyleYellow
});

var parser = new OpenLayers.Format.WKT();
var src = new OpenLayers.Projection('EPSG:4326');
var dest = new OpenLayers.Projection('EPSG:900913');

var s, wktLine, features, reprojectedFeatures;
for (s = 0; s < ytrajectory.length; s++) {
wktLine = ytrajectory[s];
features = parser.read(wktLine);
reprojectedFeatures = features.geometry.transform(src, dest);
vectorLayer.addFeatures(new OpenLayers.Feature.Vector(
reprojectedFeatures));
}

osmmap.addLayer(vectorLayer);
osmmap.zoomToExtent(vectorLayer.getDataExtent());
}

1 Answer 1

1

first you need to add a control to select the feature:

 select: new OpenLayers.Control.SelectFeature(
                vectorlayer,
                {
                    clickout: false, toggle: false,
                    multiple: false, hover: false,
                    toggleKey: "ctrlKey", // ctrl key removes from selection
                    multipleKey: "shiftKey", // shift key adds to selection
                    box: true
                }

then you need to add an event listener for the layer selection and set the popup to appear:

function onFeatureSelect(event) {
        popup = new OpenLayers.Popup.FramedCloud("chicken", 
                                 feature.geometry.getBounds().getCenterLonLat(),
                                 new OpenLayers.Size(100,100),
                                 content,
                                 null, true, onPopupClose);
        feature.popup = popup;
        map.addPopup(popup);
         }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.