[Users] Need help with inefficient code to plot vector points with no legend in tree.
Alexandre Dube
adube at mapgears.com
Mon Feb 28 14:41:04 CET 2011
Hi,
The link doesn't work for me.
Here's what I would do if I were you :
1) Outside your markTarget method, create you vector layer with its own
styleMap object (doing like you do with an array of rules is fine)
2) in you markTarget method, do :
a) pvector.removeAllFeatures();
b) create and add your feature to your vector layer (var yourNewFeature
= new OpenLayers.Feature.Vector(...) ). The "addFeature" method will
take care of drawing the feature.
That should do the trick. BTW, you don't need to create an Array(1) when
you only have one feature. To me, it looks like lots of copy/pasted code
without understanding it. Here's a bunch of samples and doc pages that
could help :
http://docs.openlayers.org/library/feature_styling.html
http://dev.openlayers.org/releases/OpenLayers-2.10/examples/vector-features.html
http://dev.openlayers.org/releases/OpenLayers-2.10/examples/stylemap.html
Alexandre
On 11-02-26 12:45 PM, Andrew Parker wrote:
> 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.
>
> _______________________________________________
> Users mailing list
> Users at geoext.org
> http://www.geoext.org/cgi-bin/mailman/listinfo/users
--
Alexandre Dubé
Mapgears
www.mapgears.com
More information about the Users
mailing list