1 | var map, layer, styleMap; |
---|
2 | OpenLayers.ProxyHost = "proxy.cgi?url="; |
---|
3 | |
---|
4 | function init() { |
---|
5 | |
---|
6 | var geographic = new OpenLayers.Projection("EPSG:4326"); |
---|
7 | var mercator = new OpenLayers.Projection("EPSG:900913"); |
---|
8 | |
---|
9 | map = new OpenLayers.Map('map', { |
---|
10 | projection: mercator |
---|
11 | }); |
---|
12 | |
---|
13 | var g = new OpenLayers.Layer.Google("Google Layer", { |
---|
14 | sphericalMercator: true |
---|
15 | }); |
---|
16 | map.addLayers([g]); |
---|
17 | |
---|
18 | // prepare to style the data |
---|
19 | styleMap = new OpenLayers.StyleMap({ |
---|
20 | strokeColor: "black", |
---|
21 | strokeWidth: 2, |
---|
22 | strokeOpacity: 0.5, |
---|
23 | fillOpacity: 0.2 |
---|
24 | }); |
---|
25 | // create a color table for state FIPS code |
---|
26 | var colors = ["red", "orange", "yellow", "green", "blue", "purple"]; |
---|
27 | var code, fips = {}; |
---|
28 | for(var i=1; i<=66; ++i) { |
---|
29 | code = "0" + i; |
---|
30 | code = code.substring(code.length - 2); |
---|
31 | fips[code] = {fillColor: colors[i % colors.length]}; |
---|
32 | } |
---|
33 | // add unique value rules with your color lookup |
---|
34 | styleMap.addUniqueValueRules("default", "STATE_FIPS", fips); |
---|
35 | |
---|
36 | // This server supports server-side reprojection, but we're using WFS 1.0 |
---|
37 | // here (which doesn't support reprojection) to illustrate client-side |
---|
38 | // reprojection. |
---|
39 | var wfs = new OpenLayers.Layer.Vector("States", { |
---|
40 | strategies: [new OpenLayers.Strategy.BBOX()], |
---|
41 | protocol: new OpenLayers.Protocol.WFS({ |
---|
42 | version: "1.0.0", |
---|
43 | srsName: "EPSG:4326", // this is the default |
---|
44 | url: "http://demo.opengeo.org/geoserver/wfs", |
---|
45 | featureType: "states", |
---|
46 | featureNS: "http://www.openplans.org/topp" |
---|
47 | }), |
---|
48 | projection: geographic, // specified because it is different than the map |
---|
49 | styleMap: styleMap |
---|
50 | }); |
---|
51 | map.addLayer(wfs); |
---|
52 | |
---|
53 | // if you want to use Geographic coords, transform to ESPG:900913 |
---|
54 | var ddBounds = new OpenLayers.Bounds( |
---|
55 | -73.839111,40.287907,-68.214111,44.441624 |
---|
56 | ); |
---|
57 | map.zoomToExtent( |
---|
58 | ddBounds.transform(geographic, mercator) |
---|
59 | ); |
---|
60 | } |
---|