[OpenLayers-Users] Adding a popup to markers

Grant Maxwell grant.maxwell at maxan.com.au
Mon Mar 25 00:43:59 PDT 2013


Hi All

I'm sure this has been asked many times and yes I've found samples but I just can't make it work. I want to have a popup when I hover the mouse over the objects on the layer.
My code is below.

I've spent several hours on this and really would appreciate some help

kind regards
Grant





	this.addVectorLayer = function(name){
		var drawControls, selectedFeature = null, selectControl = null;
		var newLayer = new OpenLayers.Layer.Vector(name);
		selectControl = new OpenLayers.Control.SelectFeature(newLayer, {
			hover: true,
			onSelect: onFeatureSelect, 
			onUnselect: onFeatureUnselect

		});

		function onPopupClose(evt) {
			selectControl.unselect(selectedFeature);
		}
		function onFeatureSelect(feature) {
			selectedFeature = feature;
			popup = new OpenLayers.Popup.FramedCloud("popup", 
					feature.geometry.getBounds().getCenterLonLat(),
					null,
					"<div style='font-size:.8em'>Feature: " + feature.id +"<br>Area: " + feature.geometry.getArea()+"</div>",
					null, true, onPopupClose);
			feature.popup = popup;
			map.addPopup(popup);
		}
		function onFeatureUnselect(feature) {
			map.removePopup(feature.popup);
			feature.popup.destroy();
			feature.popup = null;
		}    
		drawControls = {
				polygon: new OpenLayers.Control.DrawFeature(newLayer, OpenLayers.Handler.Polygon),
				select: selectControl
		};

		for(var key in drawControls) {
			map.addControl(drawControls[key]);
		}
		layers.push({"id":name, "layer":newLayer, "features":[]});
	};

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20130325/b586991c/attachment.html>


More information about the Users mailing list