[OpenLayers-Users] getting name placemark from a kml-file

Phil Scadden p.scadden at gns.cri.nz
Wed May 19 18:57:59 EDT 2010


> i want to open/show a label or popup by clicking on the icon in the map.
> this label should show the name which is written in the kml file, in this
> case the "Tractor1".
>
>    

See the KML examples but basics are - use a SelectFeature control and 
assign events to featurehighlighted and featureunhighlighted.

My onfeaturehighlighted (following the examples closely), looks like 
below. Note the feature.attributes.name
which what you want.

         function onKmlFeatureSelect(event) {
              var feature = event.feature;
                 // Since KML is user-generated, do naive protection against
                 // Javascript.
                 var content
                 if (feature.style.balloonStyle)  {
                      // Use Style.js to replace balloonstyle variables 
with attribute values
                     content = 
OpenLayers.Style.createLiteral(feature.style.balloonStyle, feature);
                 } else if (!feature.attributes.description &&  
!feature.attributes.name) {
                     content = "<table border=\"1\">\n";
                     for (var i in feature.attributes){
                          content += "<tr><td>" + feature.attributes[i] 
+ "</td></tr>\n";
                     }
                     content += "</table>\n";
                 } else {
                     content = '<table><tr bgcolor="#9CBCE2"><th>' + 
feature.attributes.name + "</th></tr></table>" + 
feature.attributes.description;
                 }
                 if (content.search("<script") != -1) {
                     content = "Content contained Javascript! Escaped 
content below.<br />" + content.replace(/</g, "&lt;");
                 }
                 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);
         }

         function onKmlFeatureUnselect(event) {
             var feature = event.feature;
             if(feature.popup) {
                 map.removePopup(feature.popup);
                 feature.popup.destroy();
                 delete feature.popup;
             }
         }
         function onPopupClose(evt) {
             if (kmlSelectControl != null)
                     kmlSelectControl.unselectAll();
         }


-- 
Phil Scadden, Senior Scientist GNS Science Ltd 764 Cumberland St, 
Private Bag 1930, Dunedin, New Zealand Ph +64 3 4799663, fax +64 3 477 5232

Notice: This email and any attachments are confidential. If received in error please destroy and immediately notify us. Do not copy or disclose the contents.




More information about the Users mailing list