[Users] Need help with inefficient code to plot vector points with no legend in tree.

Andrew Parker andrew at source3.com
Sat Feb 26 18:45:26 CET 2011


Hello all,

The test map is available at: http://satrun.nmt.edu/tests/test.html.

What I am doing is taking a geocode search and plotting it on the map 
then create a legend in the layer tree.  The next time the geocode 
search is used, the previous point is removed and the new location is 
plotted without adding another layer in the legend tree.  I am using the 
tree-builder.js that is in development by ... uh oh, where is the link?

Anyway, a portion of my code is below.  I tried playing around with 
numerous openLayers functions and below is the only code I got to work.  
To me it is seams inefficient to recreate the vector layer each time the 
function is called.  Perhaps someone can recommend more efficient code?  
pFeatures and pvector are global variables as features and vectors are 
destroyed after the function ends.  vectorNL is my variable for a vector 
that is not displayed in the legend.

The reasons I am posting here and not openLayers forum is that someone 
on this list most likely will be familiar with the tree-builder.js.

...
function markTarget(lon,lat,zoom) {
     //creating an object to hold the coordinates, the transform them.
     var projS = new OpenLayers.Projection("EPSG:4326");
     var point = new OpenLayers.LonLat(lon,lat);
     map1.setCenter(point.transform(projS, 
map1.getProjectionObject()),zoom);

     // convert projections
     var projD = new OpenLayers.Projection("EPSG:4326");
     var projS = new OpenLayers.Projection("EPSG:900913");
     var point1 = new OpenLayers.LonLat(lon,lat);
     pointX=point1.transform(projD,projS);

     var features = new Array(1);
     for (var i=0; i<features.length; i++) {
         features[i] = new OpenLayers.Feature.Vector(
             new OpenLayers.Geometry.Point(pointX.lon,pointX.lat));
     }

     var rules = [
         new OpenLayers.Rule({
             title: "Location",
             symbolizer: {
                 graphicName: "star",
                 pointRadius: 8,
                 fillColor: "#ff0000",
                 strokeColor: "#666666",
                 strokeWidth: 1
             }
         })
     ];

     // If, add the layer to the legend tree the first time ggeocoder is 
called, else plot vector only.
     if (!markerInLegend) {
         var vector = new OpenLayers.Layer.Vector('Site Marker', {
             styleMap: new OpenLayers.StyleMap(
                 new OpenLayers.Style({}, {rules: rules})
             ),
             group: "Searched Location"
         });
         vector.addFeatures(features);
         map1.addLayer(vector);
         markerInLegend = true;
         pvector=vector;
     }
     else {
         pvector.removeFeatures(pFeatures);
         var vectorNL = new 
OpenLayers.Layer.Vector('displayInLayerSwitcher:false', {
             styleMap: new OpenLayers.StyleMap(
                 new OpenLayers.Style({}, {rules: rules})
             ),
             displayInLayerSwitcher:false
         });
         vectorNL.addFeatures(features);
         map1.addLayer(vectorNL);
         pvector=vectorNL;
     };
     pFeatures=features;
}
...

Thanks for the suggestions.



More information about the Users mailing list