GreyHippo
asked on
How to add popup box to GeoJSON layer using Openlayers?
Hello,
I have a web map based on Openlayers and it will have a few GeoJSON vector layers. Â I am trying to get the popup function to work with my vector layer but I am having trouble. Â I am able to select the feature but it does not show any popup.
GeoJSON file:
Index.html:
I have a web map based on Openlayers and it will have a few GeoJSON vector layers. Â I am trying to get the popup function to work with my vector layer but I am having trouble. Â I am able to select the feature but it does not show any popup.
GeoJSON file:
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "id": 0, "properties": { "STRUCT_ID": "19-19E", "FIELD_ID": null, "Layer": "C-SSWR-MHOL", "13_165_79": "No Work" }, "geometry": { "type": "Point", "coordinates": [ -80.231324693555621, 40.66814758777484 ] } }
,
{ "type": "Feature", "id": 1, "properties": { "STRUCT_ID": "19-19D", "FIELD_ID": null, "Layer": "C-SSWR-MHOL", "13_165_79": "No Work" }, "geometry": { "type": "Point", "coordinates": [ -80.231300734788178, 40.668041857766056 ] } }
,
{ "type": "Feature", "id": 2, "properties": { "STRUCT_ID": "19-19C", "FIELD_ID": null, "Layer": "C-SSWR-MHOL", "13_165_79": "No Work" }, "geometry": { "type": "Point", "coordinates": [ -80.230900091347664, 40.667460525092594 ] } }
]
}
Index.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://openlayers.org/dev/OpenLayers.js"></script>
<script type="text/javascript">
var lon = -80.233,
lat = 40.665,
zoom = 15,
epsg4326 = new OpenLayers.Projection('EPSG:4326'),
epsg900913 = new OpenLayers.Projection('EPSG:900913');
var arrayAerial;
var select;
function init(){
var map = new OpenLayers.Map('map', {
units: 'm',
numZoomLevels: 19,
controls: [
new OpenLayers.Control.Navigation(),
new OpenLayers.Control.ScaleLine(),
new OpenLayers.Control.MousePosition(),
new OpenLayers.Control.LayerSwitcher(),
//new OpenLayers.Control.OverviewMap(),
new OpenLayers.Control.PanZoomBar()
],
projection: epsg900913,
displayProjection: epsg4326 //Is used for displaying coordinates in appropriate CRS by MousePosition control
});
arrayOSM = ["http://otile1.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg",
"http://otile2.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg",
"http://otile3.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg",
"http://otile4.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg"];
arrayAerial = ["http://otile1.mqcdn.com/tiles/1.0.0/sat/${z}/${x}/${y}.jpg",
"http://otile2.mqcdn.com/tiles/1.0.0/sat/${z}/${x}/${y}.jpg",
"http://otile3.mqcdn.com/tiles/1.0.0/sat/${z}/${x}/${y}.jpg",
"http://otile4.mqcdn.com/tiles/1.0.0/sat/${z}/${x}/${y}.jpg"];
baseOSM = new OpenLayers.Layer.OSM("MapQuest-OSM Tiles", arrayOSM);
baseAerial = new OpenLayers.Layer.OSM("MapQuest Open Aerial Tiles", arrayAerial);
var mh_layer = new OpenLayers.Layer.Vector("Manholes", {
styleMap: new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
pointRadius: 5,
fillColor: "#9de24f",
strokeColor: "#0d4cd6",
strokeWidth: 1,
strokeOpacity: 0.8 } ),
}),
projection: epsg4326,
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "./mh.geojson",
format: new OpenLayers.Format.GeoJSON({
extractAttributes: true
})
})
});
map.addLayer(baseOSM);
map.addLayer(baseAerial);
map.addLayer(mh_layer);
select = new OpenLayers.Control.SelectFeature(mh_layer);
mh_layer.events.on({
"featureselected": onFeatureSelect,
"featureunselected": onFeatureUnselect
});
map.addControl(select);
select.activate();
map.setCenter(new OpenLayers.LonLat(lon, lat).transform(epsg4326, epsg900913), zoom);
}
function onPopupClose(evt) {
select.unselectAll();
}
function onFeatureSelect(event) {
var feature = event.feature;
popup = new OpenLayers.Popup.FramedCloud(
"chicken",
feature.geometry.getBounds().getCenterLonLat(),
null,
"<h2>"+feature.attributes.STRUCT_ID + "</h2>",
null,
true,
onPopupClose);
feature.popup = popup;
map.addPopup(popup);
}
function onFeatureUnselect(event) {
var feature = event.feature;
if(feature.popup) {
map.removePopup(feature.popup);
feature.popup.destroy();
delete feature.popup;
}
}
</script>
</head>
<body onload="init()">
<div id="map" style="position: absolute; right: 0px; top: 0px; width: 100%; height: 100%"></div>
<div id="tags">
kml, popup, feature
</div>
</body>
</html>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
putting var map outside same as window["map"]
ASKER
Open in new window