This will give you the open layers map with two base layers - Google Maps and OpenStreet Map selected. This is straight enough except for the fact that you need to understand a little about Map Projections

OpenStreetMap, Google Maps, Yahoo Map etc use a projection called the Spherical Mercator projection. This treats the globe as a perfect sphere. Spherical Mercator has an official designation of EPSG:3857. You can convert a given longitude,latitude to the map projection using the OpenLayers method as shown below. For more details regarding projections kindly read http://docs.openlayers.org/library/spherical_mercator.html

Now the million dollar insight about manipulating layer is via the StyleMap. It is easy enough once you know that it is important; If you thought initially that it is something to do with Style and ignore it, then you are going to miss the bus;

What we do here is to create a Style map and tell that for 'default' rendering use the pointStyleforCluster and in-case someone selects the feature apply the style listed for 'select'
So let us see the full gamut of Style (take a peek below). There are two ways of specifying 'Style'. One via Static attributes and one via Dynamic attributes.
Take the case of pointRadius. One can set a static value to it say 50 , and set the fillColor attribute to 'blue'. Then any feature
( for the time being think about a feature as a point in the map) added to this layer will have a blue circle seen in the map;
Now what if we want to change the color of the feature based on a Custom Attribute for a feature we need to specify it as a dynamic attribute like "${fcolor}" and then in the context section add a function that takes a feature as an argument and based on some logic or attribute in the feature returns a specific color

Adding Feature to a Layer with a Custom Attribute

We can add either a simple point or a geometric shape to a Feature and then add set of Features to the Layer
(to which the style map is associated). For example if we add a Point to a feature,then the radius of the style map associated with the Layer will be taken to draw a circle with the Point as the Center.
Similarly if we create a Polygon and add it to the Feature and add the feature to the Layer , the fillColor of the Polygon will be the one associated in the Style

var features = layer.features;//If the layer has features already store it temporarily.
var polygon = OpenLayer.Geometry.Polygon.createRegularPolygon(point, 50, 22, 0);
var feature = new openLayerObj.Feature.Vector(polygon, {
fcolor: color,// this is a custom attribute of this feature
name: "Polygon"
});
features.push(feature);
layer.removeAllFeatures();// A Quirk in the OpenLayer, remove all and push all features at one go for clustering especially to work)
layer.addFeatures(features);
//OR Adding a Point and Custom Attribute
var lonlatObject = covertlatitudelong(lat, long);
var point = new OpenLayer.Geometry.Point(lonlatObject.lon, lonlatObject.lat);
var customAttr = "plan";
var feature2 = new openLayerObj.Feature.Vector(point, {
customAttr: customAttr// this is a Custom Attribute of the Feature
});
var summaryfeatures = summary_layer.features;
summary_layer.removeAllFeatures();
summaryfeatures.push(feature2);

Where to access and use the Custom Attributes added to the Feature

Okay now since we have the custom attributes and features in place let us see how to use them. Before that let us take a look at Clustering because the example I have given is heavily based on that and the custom attributes are added to the Layer with Clustering. So it would be better to explain if first. A cluster strategy is added to the Layer. Below we have a distance based simple Cluster ; If two features in the Map are within 20 pixels of each other , at any given Zoom level then it will create one new feature to represent both the separate points

Once clustering is active, every time the bound of the map gets invalidated by the user zooming or panning the features get redrawn and the functions in the context associated with the Style Map gets called.
So suppose we have only one layer which is clustered- say the SummaryLayer, and we want to print a text which has the count of the number of types of custom attribute are present in a clustered layer.
Let us see how we do it.

Here we see that we are calling a function and passing in the feature. Let us take a look at the the getClusterCount function.
The feature.cluster[i].attributes.. will give the list of feature attributes that are going to be replaced. So if we have added any custom attribute it should be added to the feature.attributes . Otherwise the next time clustering is called your Custom attribute may not be there in the clustered feature.
Next as we can see we are just accessing the value of the customAttr and incrementing a number to count it. This is returned back and printed in the label.
This same information can be send to the server via the externalGraphics url, a graph created on the fly and a png image of the graph generated and send as response to GET request of the externalGraphics URL

Popular posts from this blog

For easily monitoring the JVM metrics there is no better tool than Java VisualVM or its older counterpart JConsole; These two tools comes with the Java JDK. So it is absolutely necessary that you copy a JDK version to some temp directory in which your server runs. {Make sure that you do not install the JDK / put the java executable in the path.}

Before going in JVM monitoring, it is essential to understand a little about Java's memory model.

Note - This article is written with HotSpot JVM in mind,which is one of the most commonly used one ; implemented by OpenJDK and Oracle - formerly from Sun. This is not the only one , notable being JRockit and others; and some details would change with a different JVM.

When you are running a Java Process in a Server you allocate the Memory for the Java Virtual Machine. This memory is split to process heap { what you give withe the Xmx Xms command}where it is again subdivided on generational basis (Young, Old) and the PermGen - which is grad…

Intermittent failure in Selenium Test cases ?
After clicking the drill down sometimes web-elements are not found causing all summary table test cases to fail.
Common Root Causes
1) Prefer Selection By.ID className then By.cssSelectior and only if all else fails use By.Xpath
Selection by CSS By.CSSSelector should be preferred over XPath as this is more stable as it is natively supported by browser . XPAth is an abstraction provided by Selenium and not as performant. If XPath is used make sure that you have hand written the XPath and it is performant and not generic in that it has to to brute force search through the entire DOM to find your element.
Where ever possible , use By.ID, else By.CSSSelector and in case of no other option use XPath after proper testing.
You can use FireFinder plugin for FireFox (first add FireBug) to test your CSS or Xpath (if there is no way you can select by CSS)
For example this XPath to finding the drill down element //*[@id='scTableTest_Site-PLMN-PL…