streetmap.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * TraCINg-Server - Gathering and visualizing cyber incidents on the world
  3. *
  4. * Copyright 2013 Matthias Gazzari, Annemarie Mattmann, André Wolski
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var streetmap = function(container) {
  19. var stmap = new L.Map(container); // create a map
  20. var maxKey = 500; // maximum amount of markers
  21. var markerArray = [maxKey]; // array containing all markers
  22. var uniqueKey = 0; // unique key for marker id
  23. var holdTime = 100; // time in ms until a label disappears
  24. var incidents = 0; // total sum of incidents
  25. // set standard (start) view (first argument: lat/lng; second argument: zoom (the smaller the farther away))
  26. stmap.setView([35, 0], 3, {animate: false});
  27. // deactivate native keybindings
  28. stmap.keyboard.disable();
  29. // add layer
  30. L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
  31. attribution: "&copy; <a href='http://osm.org/copyright'>OpenStreetMap</a> contributors",
  32. noWrap: true,
  33. minZoom: 1
  34. }).addTo(stmap);
  35. /* set a bounding box for the map which is as large as the world and does not allow movement beyond
  36. * (this is sort of buggy and does not work with "setView" when the setView zoom level is the same as
  37. * the max zoom through maxBound, thus if this shall be used, set the zoom level to 4 and adjust
  38. * lat/lng if desired; enabling this also makes the Layer attribute noWrap less useful)
  39. */
  40. stmap.setMaxBounds(new L.LatLngBounds([-190, -290], [190, 290]));
  41. /*
  42. * mark incident on the map
  43. */
  44. this.addMarker = function(ll, color, label) {
  45. incidents++;
  46. // create a marker (leaflet circle)
  47. var size = 500; // 500 meter circle
  48. var latLng = new L.LatLng(ll[0], ll[1]);
  49. var circle = L.circle(latLng, size, { color: color, fillColor: color, fillOpacity: 0.5 });
  50. circle.addTo(stmap);
  51. // create a popup that will not receive focus on creation (i.e. no autoPan)
  52. var popup = new L.Popup({autoPan: false}).setContent(label);
  53. circle.bindPopup(popup);
  54. // show popup on mousehover, hide popup on mouseout
  55. var stmapHoverTimer;
  56. circle.on("mouseover", function(e) {
  57. // show standard or advanced label information depending on which is requested
  58. var splittedLabel = label.split(";");
  59. if (advInfo)
  60. this._popup.setContent(label);
  61. else
  62. this._popup.setContent(splittedLabel[0]);
  63. this.openPopup();
  64. var marker = this;
  65. // define mouseleave events
  66. popup._container.addEventListener("mouseleave", function(e) {stmapHoverTimer = setTimeout(function() {marker.closePopup()}, holdTime)});
  67. // define mouseenter events
  68. popup._container.addEventListener("mouseenter", function(e) {clearTimeout(stmapHoverTimer);});
  69. });
  70. // remove a previously defined marker with the same key
  71. removeMarker(uniqueKey);
  72. // add the circle to the marker array to be able to remove it
  73. markerArray[uniqueKey] = circle;
  74. var returnMarker = uniqueKey;
  75. uniqueKey = (uniqueKey + 1) % maxKey;
  76. return returnMarker;
  77. }
  78. /*
  79. * remove marker
  80. */
  81. function removeMarker(key) {
  82. if (markerArray[key] != undefined) {
  83. stmap.removeLayer(markerArray[key]);
  84. markerArray[key] = undefined;
  85. }
  86. }
  87. this.removeMarker = removeMarker;
  88. /*
  89. * remove all markers
  90. */
  91. this.reset = function() {
  92. for (var i = 0; i < markerArray.length; i++) {
  93. removeMarker(i);
  94. }
  95. uniqueKey = 0;
  96. incidents = 0;
  97. }
  98. /*
  99. * get marker position (lat/lng to point) for css animation
  100. */
  101. this.getPosition = function(latitude, longitude) {
  102. return stmap.latLngToContainerPoint(new L.LatLng(latitude, longitude));
  103. }
  104. /*
  105. * Zoom the streetmap
  106. */
  107. this.zoom = function(value) {
  108. stmap.zoomIn(value);
  109. }
  110. /*
  111. * Move the streetmap
  112. */
  113. this.move = function(x, y) {
  114. stmap.panBy([-x, -y]);
  115. }
  116. /*
  117. * State whether any country has at least one marker
  118. */
  119. this.hasMarker = function() {
  120. if (incidents > 0)
  121. return true;
  122. return false;
  123. }
  124. }
  125. // based on http://stackoverflow.com/questions/10762984/leaflet-map-not-displayed-properly-inside-tabbed-panel
  126. // a rather ugly solution for the "gray map on load" problem
  127. $(function() {
  128. $("body").on('shown','#StreetViewEntry', function() {
  129. stmap.invalidateSize(false);
  130. });
  131. });