facilities.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var map;
  2. var pois;
  3. var selectControl;
  4. function init() {
  5. map = new OpenLayers.Map("mapdiv");
  6. //map.addControl(new OpenLayers.Control.LayerSwitcher());
  7. var osm = new OpenLayers.Layer.OSM();
  8. /* var osm = new OpenLayers.Layer.WMS(
  9. "OpenLayers WMS", "http://vmap0.tiles.osgeo.org/wms/vmap0",
  10. {layers: 'basic'}
  11. );*/
  12. map.addLayers([osm]);
  13. update_markers();
  14. //Set start centrepoint and zoom
  15. var lonLat = new OpenLayers.LonLat( 20.263889, 63.825 )
  16. .transform(
  17. new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
  18. map.getProjectionObject() // to Spherical Mercator Projection
  19. );
  20. var zoom=9;
  21. map.setCenter (lonLat, zoom);
  22. }
  23. function update_markers() {
  24. if (pois) {
  25. while( map.popups.length ) {
  26. map.removePopup(map.popups[0]);
  27. }
  28. pois.destroyFeatures();
  29. map.removeLayer(pois);
  30. selectControl.deactivate();
  31. }
  32. selectbox = document.getElementById('btype');
  33. btype = [];
  34. for (var i=0; i<selectbox.options.length; i++) {
  35. if (selectbox.options[i].selected) {
  36. btype.push(selectbox.options[i].value);
  37. }
  38. }
  39. pois = new OpenLayers.Layer.Vector("Umeå Fritid Facilities", {
  40. strategies: [new OpenLayers.Strategy.BBOX({resFactor: 1.1})],
  41. protocol: new OpenLayers.Protocol.HTTP({
  42. url: "./filter-markers.php?btype[]=" + btype.join('&btype[]='),
  43. format: new OpenLayers.Format.Text()
  44. })
  45. });
  46. map.addLayer(pois);
  47. selectControl = new OpenLayers.Control.SelectFeature(pois);
  48. map.addControl(selectControl);
  49. selectControl.activate();
  50. pois.events.on({
  51. 'featureselected': onFeatureSelect,
  52. 'featureunselected': onFeatureUnselect
  53. });
  54. update_facility_list(btype);
  55. // Needed only for interaction, not for the display.
  56. function onPopupClose(evt) {
  57. // 'this' is the popup.
  58. var feature = this.feature;
  59. if (feature.layer) { // The feature is not destroyed
  60. selectControl.unselect(feature);
  61. } else { // After "moveend" or "refresh" events on POIs layer all
  62. // features have been destroyed by the Strategy.BBOX
  63. this.destroy();
  64. }
  65. }
  66. function onFeatureSelect(evt) {
  67. feature = evt.feature;
  68. popup = new OpenLayers.Popup.FramedCloud("featurePopup",
  69. feature.geometry.getBounds().getCenterLonLat(),
  70. new OpenLayers.Size(100,100),
  71. facility_popup_data(feature.attributes.id),
  72. null, true, onPopupClose);
  73. feature.popup = popup;
  74. popup.feature = feature;
  75. map.addPopup(popup, true);
  76. }
  77. function onFeatureUnselect(evt) {
  78. feature = evt.feature;
  79. if (feature.popup) {
  80. popup.feature = null;
  81. map.removePopup(feature.popup);
  82. feature.popup.destroy();
  83. feature.popup = null;
  84. }
  85. }
  86. }
  87. function update_facility_list(btype) {
  88. var r = new XMLHttpRequest();
  89. r.open("GET", "./facility-list.php?btype[]=" + btype.join('&btype[]='), false);
  90. r.send();
  91. var databox = document.getElementById('facility-list');
  92. databox.innerHTML = r.responseText;
  93. }
  94. function facility_popup_data(facility_id) {
  95. var r = new XMLHttpRequest();
  96. r.open("GET", "./facility-data.php?id=" + facility_id + "&popup", false);
  97. r.send();
  98. return r.responseText;
  99. }
  100. function update_facility_data(facility_id) {
  101. var r = new XMLHttpRequest();
  102. r.open("GET", "./facility-data.php?id=" + facility_id, false);
  103. r.send();
  104. var databox = document.getElementById('facility-data');
  105. databox.innerHTML = r.responseText;
  106. make_facility_popup(facility_id);
  107. }
  108. function make_facility_popup(facility_id) {
  109. // using layer 'pois'
  110. for (var i=0; i<pois.features.length; i++) {
  111. feature = pois.features[i];
  112. if (feature.attributes.id == facility_id) {
  113. map.setCenter(feature.geometry.getBounds().getCenterLonLat(), 16);
  114. selectControl.select(feature);
  115. break;
  116. }
  117. }
  118. }