RkkiiiiClickable Map Icons
------------>  Chat here, or Forums for help: http://p2pu.org/webcraft/node/12927/forums

A : Set up your map

Make sure you know how to set up an OpenLayers map on http://jsFiddle.net




B: Add your first Point
Fortunately, this part isn't any more difficult than it was to create a LonLat object:

long = 78.042068;
lat = 27.173768;
myPoint = new OpenLayers.Geometry.Point(long,lat).transform( map.displayProjection,  map.projection);



layerStyle = OpenLayers.Util.extend( { }, OpenLayers.Feature.Vector.style['default']);
pointLayer = new OpenLayers.Layer.Vector("Layer Name", {style: layerStyle});
map.addLayer(pointLayer);


sampleStyle = OpenLayers.Util.extend( { }, layerStyle);
        sampleStyle.fillColor = "blue";  // the main color of the shape
        sampleStyle.graphicName = "square";  // other good shapes are "star", "circle"
        sampleStyle.pointRadius = 10;  // controls the size of the marker
        sampleStyle.strokeColor = "blue";  // the border color of the shape
        sampleStyle.strokeWidth = 3;  // border width
        sampleStyle.fillOpacity = 1;  // from 0 (transparent) to 1 (opaque)
        sampleStyle.graphicOpacity = 1; // from 0 (transparent) to 1 (opaque)        
pointLayer.addFeatures( [ new OpenLayers.Feature.Vector(myPoint,null,sampleStyle) ] );



rotatedStyle = OpenLayers.Util.extend( { }, sampleStyle);
rotatedStyle.rotation = 45;  // 0 to 360 degrees... this doesn't work as well for circles
// create a new point called rotatedPoint
pointLayer.addFeatures( [ new OpenLayers.Feature.Vector(rotatedPoint,null,rotatedStyle) ] );

I played around with some style options to get http://jsfiddle.net/UAxun/70/

C: Add your first icon


sampleStyle.externalGraphic ="http://google-maps-icons.googlecode.com/files/palace.png";


sampleSize.pointRadius = 24;

Notice that when you change sampleSize, it also affects all styles you copied from it, so the rotatedStyle points also have this icon: http://jsfiddle.net/UAxun/71/

D: Multiple icons

dolphinIconStyle= OpenLayers.Util.extend( { }, standardStyle);
dolphinIconStyle.externalGraphic = "http://google-maps-icons.googlecode.com/files/dolphins.png";

E: Attributes

For awhile, we've been using this line to add a point:

pointLayer.addFeatures( [ new OpenLayers.Feature.Vector(rotatedPoint,null,rotatedStyle) ] );


// create myPoint and myStyle before this line
myPointFeature = new OpenLayers.Feature.Vector(myPoint,null,myStyle)
pointLayer.addFeatures( [ myPointFeature ] );


osm = { fullName: "OpenStreetMap", isOpen: true, homepage:"http://openstreetmap.org" } ;
bng = { fullName: "Bing Maps", isOpen: false, homepage:"http://maps.bing.com" } ;

Now we can check these properties. For example, osm.isOpen = true, and window.location=bng.homepage will send your browser to maps.bing.com


myPointFeature = new OpenLayers.Feature.Vector(myPoint,null,myStyle)
myPointFeature.attributes = { 
      name: "Taj Mahal",
      description: "One of India's most famous buildings",
      wikiPage: "http://simple.wikipedia.org/wiki/Taj_Mahal "
};
pointLayer.addFeatures( [ myPointFeature ] );


F: Clicks and good popups make an interactive map


// create a Control that watches for clicking on a feature, then add the control to the map
selectControl = new OpenLayers.Control.SelectFeature( pointLayer );
map.addControl(selectControl);
selectControl.activate();

/* the control calls events (featureselected and featureunselected) and we connect those events to functions */
pointLayer.events.on({
    'featureselected': onFeatureSelect,
    'featureunselected': onFeatureUnselect
});


function onFeatureSelect(clickInfo) {
        clickedFeature = clickInfo.feature;
        popup = new OpenLayers.Popup.FramedCloud(
                "featurePopup",  // an ID for this popup
                clickedFeature.geometry.getBounds().getCenterLonLat(), // anchor to the icon
                new OpenLayers.Size(120,250),  // set height and width of the window
                clickedFeature.attributes.name,  // HTML displayed inside the window
                null,                 // used for other applications
                true,                 // have an [x] to close the Popup
                onPopupClose  // call another function when the Popup is closed
        );
        // let feature and popup 'know' about each other
        clickedFeature.popup = popup;
        popup.feature = clickedFeature;
        // add the popup to the map
        map.addPopup(popup);
}
function onFeatureUnselect(clickInfo) {
        feature = clickInfo.feature;
        if (feature.popup) {
                // if an icon with a popup window is un-selected, that popup is removed
                popup.feature = null;
                map.removePopup(feature.popup);
                feature.popup.destroy();
                feature.popup = null;
        }
}
function onPopupClose(closeInfo) {
        // closing the Popup un-selects the icon
        selectControl.unselect(this.feature);
}


------------>  Go to the forums for help: http://p2pu.org/webcraft/node/12927/forums

Once you have that working...


clickedFeature.attributes.name,


"<h3 style='font-weight:bold;color:red;'>" + clickedFeature.attributes.name + "</h3>" + clickedFeature.attributes.description,

Make sure that the line still ends with a comma.

If things go wrong, use your browser's built-in debugger to see if you used the wrong variable name or punctuation.

http://jsfiddle.net/UAxun/73/


<a href="http://example.com?TheLinkURL" target="_blank">Link Text</a>


<img src="http://example.com/ImageURL.jpg"/>