mapParser.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="http://cdn.leafletjs.com/leaflet-0.7/leaflet.js"></script>
  5. <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7/leaflet.css" />
  6. <script src="leaflet-providers/leaflet-providers.js"></script>
  7. <script src="Leaflet-MiniMap/src/Control.MiniMap.js"></script>
  8. <link rel="stylesheet" href="Leaflet-MiniMap/src/Control.MiniMap.css" />
  9. <script type="text/javascript" charset="utf-8">
  10. // Function to parse the URL search query parameters.
  11. // It will give A, B, C given http://www.somelink.com?lat=A&lon=B&zoom=C
  12. // See "http://javascriptproductivity.blogspot.com/" +
  13. // "2013/02/get-url-variables-with-javascript.html".
  14. function getParams(){
  15. // Make an object variable to hold
  16. // the parsed URL parameters' keys and vlaues.
  17. var params = {};
  18. // Remove the '?' character after the base url.
  19. // x.substring(i) will return the substring of x starting at index i
  20. // up to the end of the string.
  21. // Drupal CMS might have a path like this (two '?' characters):
  22. // mapParser.html?t=E3OD?lat=14.6760413&lon=121.0437003&...
  23. // We need to handle this case also.
  24. var lastIndex = window.location.search.lastIndexOf("?")
  25. // Get the substring not including any '?' character.
  26. var query_string = window.location.search.substring(lastIndex + 1);
  27. // Explode the string using the '&' character.
  28. var query_string_parts = query_string.split('&');
  29. // Traverse the exploded tokens.
  30. for(i in query_string_parts) {
  31. // Explode the string using '=' to isolate keys and values.
  32. key_value = query_string_parts[i].split('=');
  33. // Insert a new key and set it to the current parsed value.
  34. params[key_value[0]] = key_value[1];
  35. }
  36. // Return the parameter object contianing the keys and values.
  37. return params;
  38. }
  39. </script>
  40. <style>
  41. .leaflet-popup-content {
  42. text-align: center;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div id='map_container' style="width:95%px; height:590px;" data-zoom=""></div>
  48. <script type="text/javascript">
  49. // Get the DOM node to which we will append the map.
  50. // In our setup the map container is a DIV element.
  51. var mapDiv = document.getElementById("map_container");
  52. // Parse the query parameters in the URL to obtain the
  53. // LAT, LON, and ZOOM values.
  54. queryString = getParams();
  55. // Convert the parameters to their numeric equivalent.
  56. // Otherwise, it will not work.
  57. var latitude = Number(queryString["lat"]);
  58. var longitude = Number(queryString["lon"]);
  59. // Retrieve and adjust the map's height by 10px for better layout.
  60. var mapHeight = Number(queryString["height"]) - 10;
  61. mapDiv.style.height = String(mapHeight) + "px";
  62. // Retrieve the zoom level.
  63. var zoom = Number(queryString["zoom"]);
  64. // Retrive the popup text that will be used by the marker.
  65. var popUpText = queryString["text"];
  66. // Retrieve the Base Map's Tile to be used.
  67. var tile = queryString["tile"];
  68. // Retrieve the information about the inclusion of Overview map.
  69. var minimap = queryString["minimap"];
  70. // Create a map in the the target DOM container.
  71. // Set the view to a given place and zoom value.
  72. var map = L.map('map_container').setView([latitude, longitude], zoom);
  73. // Choose the Map Tile provider "Esri.WorldTopoMap"
  74. var mapTileProvider = tile;
  75. // Set the base map.
  76. var mapTile = L.tileLayer.provider(mapTileProvider, { attribution: "Map Tile: " + mapTileProvider });
  77. mapTile.addTo(map);
  78. // Add a marker in the given location.
  79. var p1 = L.marker([latitude, longitude]);
  80. p1.addTo(map);
  81. // If the pop-up text is specified.
  82. if (popUpText != '') {
  83. // Replace the %20 HTML Entity by an empty space.
  84. // %20 is the result of texts being piped via URL.
  85. popUpText = popUpText.replace(/%20/g, ' ');
  86. // Bind the text to the current marker.
  87. p1.bindPopup(popUpText);
  88. }
  89. // Check if MiniMap needs to be shown also.
  90. if (minimap == "on") {
  91. // Create a new Tile Layer for the MiniMap.
  92. // Per documentation, do not reuse the existing Tile object
  93. // to avoid rendering issues.
  94. var mapTile2 = L.tileLayer.provider(mapTileProvider);
  95. var miniMap = new L.Control.MiniMap(mapTile2).addTo(map);
  96. }
  97. // Initialize the zoom data attribute.
  98. mapDiv.setAttribute("data-zoom", map.getZoom());
  99. // Add an event listener that will keep track the changes in the
  100. // map's zoom levels and it will update the data attribute accordingly.
  101. map.on('zoomend', function(e) {
  102. mapDiv.setAttribute("data-zoom", map.getZoom());
  103. });
  104. </script>
  105. </body>
  106. </html>