Skip to content

Instantly share code, notes, and snippets.

@RCura
Created July 29, 2013 16:19
Show Gist options
  • Select an option

  • Save RCura/6105524 to your computer and use it in GitHub Desktop.

Select an option

Save RCura/6105524 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
var map, selectControl, selectedFeature;
function onPopupClose(evt) {
selectControl.unselect(selectedFeature);
};
function onFeatureSelect(feature) {
selectedFeature = feature;
var str = "<table style=\'font-size: 1.5em\'><tr><th colspan=2>"+feature.layer.name+"</th></tr>";
for (attr in feature.attributes){
str = str + "<tr><td>"+attr+"</td><td>"+feature.attributes[attr]+"</td></tr>";
};
str = str + "</table>" ;
popup = new OpenLayers.Popup.FramedCloud("chicken",
feature.geometry.getBounds().getCenterLonLat(),
null,
"<div style=\'font-size:.5em\'>"+str+"</div>",
null, true, onPopupClose);
feature.popup = popup;
map.addPopup(popup);
};
function onFeatureUnselect(feature) {
map.removePopup(feature.popup);
feature.popup.destroy();
feature.popup = null;
};
function init(){
var options = {
projection: new OpenLayers.Projection("EPSG:4326"),
controls: [new OpenLayers.Control.Navigation(),
new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.LayerSwitcher(),
new OpenLayers.Control.Permalink(),
new OpenLayers.Control.ScaleLine(),
new OpenLayers.Control.MousePosition() ]
};
map = new OpenLayers.Map("map", options);
var baseLayer = new OpenLayers.Layer.WMS( "OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
map.addLayer(baseLayer);
var States = new OpenLayers.Layer.Vector("States");
var StatesOptions = {
projection: "EPSG: 4326",
strategies: [new OpenLayers.Strategy.Fixed()],
styleMap: new OpenLayers.StyleMap({'pointRadius': '${Murder}',
'fillColor': '${color}',
'strokeColor': 'black',
'fillOpacity': '0.4'
})
};
map.addLayer(States);
var StatesFeatures = {
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "id": 0, "properties": { "Population": 3615.0, "Income": 3624.0, "Illiteracy": 2.1, "Life.Exp": 69.05, "Murder": 15.1, "HS.Grad": 41.3, "Frost": 20.0, "Area": 50708.0, "Name": "Alabama", "color": "#FF0000" }, "geometry": { "type": "Point", "coordinates": [ -86.7509, 32.5901 ] } },
{ "type": "Feature", "id": 1, "properties": { "Population": 365.0, "Income": 6315.0, "Illiteracy": 1.5, "Life.Exp": 69.31, "Murder": 11.3, "HS.Grad": 66.7, "Frost": 152.0, "Area": 566432.0, "Name": "Alaska", "color": "#FF1F00" }, "geometry": { "type": "Point", "coordinates": [ -127.25, 49.25 ] } },
{ "type": "Feature", "id": 2, "properties": { "Population": 2212.0, "Income": 4530.0, "Illiteracy": 1.8, "Life.Exp": 70.55, "Murder": 7.8, "HS.Grad": 58.1, "Frost": 15.0, "Area": 113417.0, "Name": "Arizona", "color": "#FF3D00" }, "geometry": { "type": "Point", "coordinates": [ -111.625, 34.2192 ] } },
{ "type": "Feature", "id": 3, "properties": { "Population": 2110.0, "Income": 3378.0, "Illiteracy": 1.9, "Life.Exp": 70.66, "Murder": 10.1, "HS.Grad": 39.9, "Frost": 65.0, "Area": 51945.0, "Name": "Arkansas", "color": "#FF5C00" }, "geometry": { "type": "Point", "coordinates": [ -92.2992, 34.7336 ] } },
{ "type": "Feature", "id": 4, "properties": { "Population": 21198.0, "Income": 5114.0, "Illiteracy": 1.1, "Life.Exp": 71.71, "Murder": 10.3, "HS.Grad": 62.6, "Frost": 20.0, "Area": 156361.0, "Name": "California", "color": "#FF7A00" }, "geometry": { "type": "Point", "coordinates": [ -119.773, 36.5341 ] } },
{ "type": "Feature", "id": 5, "properties": { "Population": 2541.0, "Income": 4884.0, "Illiteracy": 0.7, "Life.Exp": 72.06, "Murder": 6.8, "HS.Grad": 63.9, "Frost": 166.0, "Area": 103766.0, "Name": "Colorado", "color": "#FF9900" }, "geometry": { "type": "Point", "coordinates": [ -105.513, 38.6777 ] } },
{ "type": "Feature", "id": 6, "properties": { "Population": 3100.0, "Income": 5348.0, "Illiteracy": 1.1, "Life.Exp": 72.48, "Murder": 3.1, "HS.Grad": 56.0, "Frost": 139.0, "Area": 4862.0, "Name": "Connecticut", "color": "#FFB800" }, "geometry": { "type": "Point", "coordinates": [ -72.3573, 41.5928 ] } },
{ "type": "Feature", "id": 7, "properties": { "Population": 579.0, "Income": 4809.0, "Illiteracy": 0.9, "Life.Exp": 70.06, "Murder": 6.2, "HS.Grad": 54.6, "Frost": 103.0, "Area": 1982.0, "Name": "Delaware", "color": "#FFD600" }, "geometry": { "type": "Point", "coordinates": [ -74.9841, 38.6777 ] } },
{ "type": "Feature", "id": 8, "properties": { "Population": 8277.0, "Income": 4815.0, "Illiteracy": 1.3, "Life.Exp": 70.66, "Murder": 10.7, "HS.Grad": 52.6, "Frost": 11.0, "Area": 54090.0, "Name": "Florida", "color": "#FFF500" }, "geometry": { "type": "Point", "coordinates": [ -81.685, 27.8744 ] } },
{ "type": "Feature", "id": 9, "properties": { "Population": 4931.0, "Income": 4091.0, "Illiteracy": 2.0, "Life.Exp": 68.54, "Murder": 13.9, "HS.Grad": 40.6, "Frost": 60.0, "Area": 58073.0, "Name": "Georgia", "color": "#EBFF00" }, "geometry": { "type": "Point", "coordinates": [ -83.3736, 32.3329 ] } },
{ "type": "Feature", "id": 10, "properties": { "Population": 868.0, "Income": 4963.0, "Illiteracy": 1.9, "Life.Exp": 73.6, "Murder": 6.2, "HS.Grad": 61.9, "Frost": 0.0, "Area": 6425.0, "Name": "Hawaii", "color": "#CCFF00" }, "geometry": { "type": "Point", "coordinates": [ -126.25, 31.75 ] } },
{ "type": "Feature", "id": 11, "properties": { "Population": 813.0, "Income": 4119.0, "Illiteracy": 0.6, "Life.Exp": 71.87, "Murder": 5.3, "HS.Grad": 59.5, "Frost": 126.0, "Area": 82677.0, "Name": "Idaho", "color": "#ADFF00" }, "geometry": { "type": "Point", "coordinates": [ -113.93, 43.5648 ] } },
{ "type": "Feature", "id": 12, "properties": { "Population": 11197.0, "Income": 5107.0, "Illiteracy": 0.9, "Life.Exp": 70.14, "Murder": 10.3, "HS.Grad": 52.6, "Frost": 127.0, "Area": 55748.0, "Name": "Illinois", "color": "#8FFF00" }, "geometry": { "type": "Point", "coordinates": [ -89.3776, 40.0495 ] } },
{ "type": "Feature", "id": 13, "properties": { "Population": 5313.0, "Income": 4458.0, "Illiteracy": 0.7, "Life.Exp": 70.88, "Murder": 7.1, "HS.Grad": 52.9, "Frost": 122.0, "Area": 36097.0, "Name": "Indiana", "color": "#70FF00" }, "geometry": { "type": "Point", "coordinates": [ -86.0808, 40.0495 ] } },
{ "type": "Feature", "id": 14, "properties": { "Population": 2861.0, "Income": 4628.0, "Illiteracy": 0.5, "Life.Exp": 72.56, "Murder": 2.3, "HS.Grad": 59.0, "Frost": 140.0, "Area": 55941.0, "Name": "Iowa", "color": "#52FF00" }, "geometry": { "type": "Point", "coordinates": [ -93.3714, 41.9358 ] } },
{ "type": "Feature", "id": 15, "properties": { "Population": 2280.0, "Income": 4669.0, "Illiteracy": 0.6, "Life.Exp": 72.58, "Murder": 4.5, "HS.Grad": 59.9, "Frost": 114.0, "Area": 81787.0, "Name": "Kansas", "color": "#33FF00" }, "geometry": { "type": "Point", "coordinates": [ -98.1156, 38.4204 ] } },
{ "type": "Feature", "id": 16, "properties": { "Population": 3387.0, "Income": 3712.0, "Illiteracy": 1.6, "Life.Exp": 70.1, "Murder": 10.6, "HS.Grad": 38.5, "Frost": 95.0, "Area": 39650.0, "Name": "Kentucky", "color": "#14FF00" }, "geometry": { "type": "Point", "coordinates": [ -84.7674, 37.3915 ] } },
{ "type": "Feature", "id": 17, "properties": { "Population": 3806.0, "Income": 3545.0, "Illiteracy": 2.8, "Life.Exp": 68.76, "Murder": 13.2, "HS.Grad": 42.2, "Frost": 12.0, "Area": 44930.0, "Name": "Louisiana", "color": "#00FF0A" }, "geometry": { "type": "Point", "coordinates": [ -92.2724, 30.6181 ] } },
{ "type": "Feature", "id": 18, "properties": { "Population": 1058.0, "Income": 3694.0, "Illiteracy": 0.7, "Life.Exp": 70.39, "Murder": 2.7, "HS.Grad": 54.7, "Frost": 161.0, "Area": 30920.0, "Name": "Maine", "color": "#00FF29" }, "geometry": { "type": "Point", "coordinates": [ -68.9801, 45.6226 ] } },
{ "type": "Feature", "id": 19, "properties": { "Population": 4122.0, "Income": 5299.0, "Illiteracy": 0.9, "Life.Exp": 70.22, "Murder": 8.5, "HS.Grad": 52.3, "Frost": 101.0, "Area": 9891.0, "Name": "Maryland", "color": "#00FF47" }, "geometry": { "type": "Point", "coordinates": [ -76.6459, 39.2778 ] } },
{ "type": "Feature", "id": 20, "properties": { "Population": 5814.0, "Income": 4755.0, "Illiteracy": 1.1, "Life.Exp": 71.83, "Murder": 3.3, "HS.Grad": 58.5, "Frost": 103.0, "Area": 7826.0, "Name": "Massachusetts", "color": "#00FF66" }, "geometry": { "type": "Point", "coordinates": [ -71.58, 42.3645 ] } },
{ "type": "Feature", "id": 21, "properties": { "Population": 9111.0, "Income": 4751.0, "Illiteracy": 0.9, "Life.Exp": 70.63, "Murder": 11.1, "HS.Grad": 52.8, "Frost": 125.0, "Area": 56817.0, "Name": "Michigan", "color": "#00FF85" }, "geometry": { "type": "Point", "coordinates": [ -84.687, 43.1361 ] } },
{ "type": "Feature", "id": 22, "properties": { "Population": 3921.0, "Income": 4675.0, "Illiteracy": 0.6, "Life.Exp": 72.96, "Murder": 2.3, "HS.Grad": 57.6, "Frost": 160.0, "Area": 79289.0, "Name": "Minnesota", "color": "#00FFA3" }, "geometry": { "type": "Point", "coordinates": [ -94.6043, 46.3943 ] } },
{ "type": "Feature", "id": 23, "properties": { "Population": 2341.0, "Income": 3098.0, "Illiteracy": 2.4, "Life.Exp": 68.09, "Murder": 12.5, "HS.Grad": 41.0, "Frost": 50.0, "Area": 47296.0, "Name": "Mississippi", "color": "#00FFC2" }, "geometry": { "type": "Point", "coordinates": [ -89.8065, 32.6758 ] } },
{ "type": "Feature", "id": 24, "properties": { "Population": 4767.0, "Income": 4254.0, "Illiteracy": 0.8, "Life.Exp": 70.69, "Murder": 9.3, "HS.Grad": 48.8, "Frost": 108.0, "Area": 68995.0, "Name": "Missouri", "color": "#00FFE0" }, "geometry": { "type": "Point", "coordinates": [ -92.5137, 38.3347 ] } },
{ "type": "Feature", "id": 25, "properties": { "Population": 746.0, "Income": 4347.0, "Illiteracy": 0.6, "Life.Exp": 70.56, "Murder": 5.0, "HS.Grad": 59.2, "Frost": 155.0, "Area": 145587.0, "Name": "Montana", "color": "#00FFFF" }, "geometry": { "type": "Point", "coordinates": [ -109.32, 46.823 ] } },
{ "type": "Feature", "id": 26, "properties": { "Population": 1544.0, "Income": 4508.0, "Illiteracy": 0.6, "Life.Exp": 72.6, "Murder": 2.9, "HS.Grad": 59.3, "Frost": 139.0, "Area": 76483.0, "Name": "Nebraska", "color": "#00E0FF" }, "geometry": { "type": "Point", "coordinates": [ -99.5898, 41.3356 ] } },
{ "type": "Feature", "id": 27, "properties": { "Population": 590.0, "Income": 5149.0, "Illiteracy": 0.5, "Life.Exp": 69.03, "Murder": 11.5, "HS.Grad": 65.2, "Frost": 188.0, "Area": 109889.0, "Name": "Nevada", "color": "#00C2FF" }, "geometry": { "type": "Point", "coordinates": [ -116.851, 39.1063 ] } },
{ "type": "Feature", "id": 28, "properties": { "Population": 812.0, "Income": 4281.0, "Illiteracy": 0.7, "Life.Exp": 71.23, "Murder": 3.3, "HS.Grad": 57.6, "Frost": 174.0, "Area": 9027.0, "Name": "New Hampshire", "color": "#00A3FF" }, "geometry": { "type": "Point", "coordinates": [ -71.3924, 43.3934 ] } },
{ "type": "Feature", "id": 29, "properties": { "Population": 7333.0, "Income": 5237.0, "Illiteracy": 1.1, "Life.Exp": 70.93, "Murder": 5.2, "HS.Grad": 52.5, "Frost": 115.0, "Area": 7521.0, "Name": "New Jersey", "color": "#0085FF" }, "geometry": { "type": "Point", "coordinates": [ -74.2336, 39.9637 ] } },
{ "type": "Feature", "id": 30, "properties": { "Population": 1144.0, "Income": 3601.0, "Illiteracy": 2.2, "Life.Exp": 70.32, "Murder": 9.7, "HS.Grad": 55.2, "Frost": 120.0, "Area": 121412.0, "Name": "New Mexico", "color": "#0066FF" }, "geometry": { "type": "Point", "coordinates": [ -105.942, 34.4764 ] } },
{ "type": "Feature", "id": 31, "properties": { "Population": 18076.0, "Income": 4903.0, "Illiteracy": 1.4, "Life.Exp": 70.55, "Murder": 10.9, "HS.Grad": 52.7, "Frost": 82.0, "Area": 47831.0, "Name": "New York", "color": "#0047FF" }, "geometry": { "type": "Point", "coordinates": [ -75.1449, 43.1361 ] } },
{ "type": "Feature", "id": 32, "properties": { "Population": 5441.0, "Income": 3875.0, "Illiteracy": 1.8, "Life.Exp": 69.21, "Murder": 11.1, "HS.Grad": 38.5, "Frost": 80.0, "Area": 48798.0, "Name": "North Carolina", "color": "#0029FF" }, "geometry": { "type": "Point", "coordinates": [ -78.4686, 35.4195 ] } },
{ "type": "Feature", "id": 33, "properties": { "Population": 637.0, "Income": 5087.0, "Illiteracy": 0.8, "Life.Exp": 72.78, "Murder": 1.4, "HS.Grad": 50.3, "Frost": 186.0, "Area": 69273.0, "Name": "North Dakota", "color": "#000AFF" }, "geometry": { "type": "Point", "coordinates": [ -100.099, 47.2517 ] } },
{ "type": "Feature", "id": 34, "properties": { "Population": 10735.0, "Income": 4561.0, "Illiteracy": 0.8, "Life.Exp": 70.82, "Murder": 7.4, "HS.Grad": 53.2, "Frost": 124.0, "Area": 40975.0, "Name": "Ohio", "color": "#1400FF" }, "geometry": { "type": "Point", "coordinates": [ -82.5963, 40.221 ] } },
{ "type": "Feature", "id": 35, "properties": { "Population": 2715.0, "Income": 3983.0, "Illiteracy": 1.1, "Life.Exp": 71.42, "Murder": 6.4, "HS.Grad": 51.6, "Frost": 82.0, "Area": 68782.0, "Name": "Oklahoma", "color": "#3300FF" }, "geometry": { "type": "Point", "coordinates": [ -97.1239, 35.5053 ] } },
{ "type": "Feature", "id": 36, "properties": { "Population": 2284.0, "Income": 4660.0, "Illiteracy": 0.6, "Life.Exp": 72.13, "Murder": 4.2, "HS.Grad": 60.0, "Frost": 44.0, "Area": 96184.0, "Name": "Oregon", "color": "#5200FF" }, "geometry": { "type": "Point", "coordinates": [ -120.068, 43.9078 ] } },
{ "type": "Feature", "id": 37, "properties": { "Population": 11860.0, "Income": 4449.0, "Illiteracy": 1.0, "Life.Exp": 70.43, "Murder": 6.1, "HS.Grad": 50.2, "Frost": 126.0, "Area": 44966.0, "Name": "Pennsylvania", "color": "#7000FF" }, "geometry": { "type": "Point", "coordinates": [ -77.45, 40.9069 ] } },
{ "type": "Feature", "id": 38, "properties": { "Population": 931.0, "Income": 4558.0, "Illiteracy": 1.3, "Life.Exp": 71.9, "Murder": 2.4, "HS.Grad": 46.4, "Frost": 127.0, "Area": 1049.0, "Name": "Rhode Island", "color": "#8F00FF" }, "geometry": { "type": "Point", "coordinates": [ -71.1244, 41.5928 ] } },
{ "type": "Feature", "id": 39, "properties": { "Population": 2816.0, "Income": 3635.0, "Illiteracy": 2.3, "Life.Exp": 67.96, "Murder": 11.6, "HS.Grad": 37.8, "Frost": 65.0, "Area": 30225.0, "Name": "South Carolina", "color": "#AD00FF" }, "geometry": { "type": "Point", "coordinates": [ -80.5056, 33.619 ] } },
{ "type": "Feature", "id": 40, "properties": { "Population": 681.0, "Income": 4167.0, "Illiteracy": 0.5, "Life.Exp": 72.08, "Murder": 1.7, "HS.Grad": 53.3, "Frost": 172.0, "Area": 75955.0, "Name": "South Dakota", "color": "#CC00FF" }, "geometry": { "type": "Point", "coordinates": [ -99.7238, 44.3365 ] } },
{ "type": "Feature", "id": 41, "properties": { "Population": 4173.0, "Income": 3821.0, "Illiteracy": 1.7, "Life.Exp": 70.11, "Murder": 11.0, "HS.Grad": 41.8, "Frost": 70.0, "Area": 41328.0, "Name": "Tennessee", "color": "#EB00FF" }, "geometry": { "type": "Point", "coordinates": [ -86.456, 35.6767 ] } },
{ "type": "Feature", "id": 42, "properties": { "Population": 12237.0, "Income": 4188.0, "Illiteracy": 2.2, "Life.Exp": 70.9, "Murder": 12.2, "HS.Grad": 47.4, "Frost": 35.0, "Area": 262134.0, "Name": "Texas", "color": "#FF00F5" }, "geometry": { "type": "Point", "coordinates": [ -98.7857, 31.3897 ] } },
{ "type": "Feature", "id": 43, "properties": { "Population": 1203.0, "Income": 4022.0, "Illiteracy": 0.6, "Life.Exp": 72.9, "Murder": 4.5, "HS.Grad": 67.3, "Frost": 137.0, "Area": 82096.0, "Name": "Utah", "color": "#FF00D6" }, "geometry": { "type": "Point", "coordinates": [ -111.33, 39.1063 ] } },
{ "type": "Feature", "id": 44, "properties": { "Population": 472.0, "Income": 3907.0, "Illiteracy": 0.6, "Life.Exp": 71.64, "Murder": 5.5, "HS.Grad": 57.1, "Frost": 168.0, "Area": 9267.0, "Name": "Vermont", "color": "#FF00B8" }, "geometry": { "type": "Point", "coordinates": [ -72.545, 44.2508 ] } },
{ "type": "Feature", "id": 45, "properties": { "Population": 4981.0, "Income": 4701.0, "Illiteracy": 1.4, "Life.Exp": 70.08, "Murder": 9.5, "HS.Grad": 47.8, "Frost": 85.0, "Area": 39780.0, "Name": "Virginia", "color": "#FF0099" }, "geometry": { "type": "Point", "coordinates": [ -78.2005, 37.563 ] } },
{ "type": "Feature", "id": 46, "properties": { "Population": 3559.0, "Income": 4864.0, "Illiteracy": 0.6, "Life.Exp": 71.72, "Murder": 4.3, "HS.Grad": 63.5, "Frost": 32.0, "Area": 66570.0, "Name": "Washington", "color": "#FF007A" }, "geometry": { "type": "Point", "coordinates": [ -119.746, 47.4231 ] } },
{ "type": "Feature", "id": 47, "properties": { "Population": 1799.0, "Income": 3617.0, "Illiteracy": 1.4, "Life.Exp": 69.48, "Murder": 6.7, "HS.Grad": 41.6, "Frost": 100.0, "Area": 24070.0, "Name": "West Virginia", "color": "#FF005C" }, "geometry": { "type": "Point", "coordinates": [ -80.6665, 38.4204 ] } },
{ "type": "Feature", "id": 48, "properties": { "Population": 4589.0, "Income": 4468.0, "Illiteracy": 0.7, "Life.Exp": 72.48, "Murder": 3.0, "HS.Grad": 54.5, "Frost": 149.0, "Area": 54464.0, "Name": "Wisconsin", "color": "#FF003D" }, "geometry": { "type": "Point", "coordinates": [ -89.9941, 44.5937 ] } },
{ "type": "Feature", "id": 49, "properties": { "Population": 376.0, "Income": 4566.0, "Illiteracy": 0.6, "Life.Exp": 70.29, "Murder": 6.9, "HS.Grad": 62.9, "Frost": 173.0, "Area": 97203.0, "Name": "Wyoming", "color": "#FF001F" }, "geometry": { "type": "Point", "coordinates": [ -107.256, 43.0504 ] } }
]
}
;
States.addFeatures(new OpenLayers.Format.GeoJSON().read(StatesFeatures));
States.addOptions(StatesOptions);
selectControl = new OpenLayers.Control.SelectFeature(
[States],
{onSelect: onFeatureSelect, onUnselect: onFeatureUnselect}
);
map.addControl(selectControl);
selectControl.activate();
map.zoomToExtent( new OpenLayers.Bounds(-127.25,27.8744,-68.9801,49.25));
};
function showStatus(text) {
document.getElementById("status").innerHTML = text;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment