-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmapParser.html
executable file
·180 lines (146 loc) · 6.31 KB
/
mapParser.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/leaflet.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/leaflet.css" />
<script src="leaflet-providers/leaflet-providers.js"></script>
<script src="Leaflet-MiniMap/src/Control.MiniMap.js"></script>
<link rel="stylesheet" href="Leaflet-MiniMap/src/Control.MiniMap.css" />
<script type="text/javascript" charset="utf-8">
// Function to parse the URL search query parameters.
// It will give A, B, C given http://www.somelink.com?lat=A&lon=B&zoom=C
// See "http://javascriptproductivity.blogspot.com/" +
// "2013/02/get-url-variables-with-javascript.html".
function getParams(){
// Make an object variable to hold
// the parsed URL parameters' keys and vlaues.
var params = {};
// Remove the '?' character after the base url.
// x.substring(i) will return the substring of x starting at index i
// up to the end of the string.
// Drupal CMS might have a path like this (two '?' characters):
// mapParser.html?t=E3OD?lat=14.6760413&lon=121.0437003&...
// We need to handle this case also.
var lastIndex = window.location.search.lastIndexOf("?")
// Get the substring not including any '?' character.
var query_string = window.location.search.substring(lastIndex + 1);
// Explode the string using the '&' character.
var query_string_parts = query_string.split('&');
// Traverse the exploded tokens.
for(i in query_string_parts) {
// Explode the string using '=' to isolate keys and values.
key_value = query_string_parts[i].split('=');
// Insert a new key and set it to the current parsed value.
params[key_value[0]] = key_value[1];
}
// Return the parameter object contianing the keys and values.
return params;
}
</script>
<style>
.leaflet-popup-content {
text-align: center;
}
body {
padding: 0;
margin: 0;
}
html, body, #map_container {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id='map_container' data-lat="" data-lon="" data-zoom=""></div>
<script type="text/javascript">
// Get the DOM node to which we will append the map.
// In our setup the map container is a DIV element.
var mapDiv = document.getElementById("map_container");
// Parse the query parameters in the URL to obtain the
// LAT, LON, and ZOOM values.
var queryString = getParams();
// Fetch the most important attributes.
var latitude = queryString["lat"];
var longitude= queryString["lon"];
var zoom = queryString["zoom"];
// Initialize the mapDiv data attributes.
mapDiv.setAttribute("data-lat", latitude);
mapDiv.setAttribute("data-lon", longitude);
mapDiv.setAttribute("data-zoom", zoom);
// Convert the parameters to their numeric equivalent.
// Otherwise, initializing the Leaflet map object will not work.
latitude = Number(latitude);
longitude = Number(longitude);
zoom = Number(zoom);
var responsive = queryString["responsive"];
// If responsive map option is enabled, set the map height to 100%.
if (responsive == "on") {
mapDiv.style.height = "100%";
}
else {
// Retrieve the map's height.
var mapHeight = Number(queryString["height"]);
mapDiv.style.height = String(mapHeight) + "px";
}
// Retrive the popup text that will be used by the marker.
var popUpText = decodeURIComponent(queryString["text"]);
// Retrieve the Base Map's Tile to be used.
var tile = queryString["tile"];
// Retrieve the information about the inclusion of Overview map.
var minimap = queryString["minimap"];
// Create a map in the the target DOM container.
// Set the view to a given place and zoom value.
var map = L.map('map_container').setView([latitude, longitude], zoom);
// Choose the Map Tile provider e.g. "Esri.WorldTopoMap"
var mapTileProvider = tile;
var restrictedTiles = ['MapQuestOpen.Aerial', 'MapQuestOpen.OSM'];
// Redirect MapQuest tiles to OpenStreetMap tiles.
if (restrictedTiles.indexOf(mapTileProvider) != -1) {
mapTileProvider = 'OpenStreetMap.Mapnik';
}
// Set the base map.
var mapTile = L.tileLayer.provider(mapTileProvider, { attribution: "Map Tile: " + mapTileProvider });
mapTile.addTo(map);
// Add a marker in the given location.
// Make it draggble so that user could refine the position visually.
var p1 = L.marker([latitude, longitude], {
draggable: true,
});
p1.addTo(map);
// Add an event listener that will keep track the changes in the
// marker location and will update the data attribute accordingly.
p1.on('dragend', function(e) {
var newLocation = this.getLatLng();
var lat = newLocation.lat;
var lon = newLocation.lng;
// Pan the map to its new center which is the last position
// of the dragged marker.
map.panTo([lat, lon]);
mapDiv.setAttribute("data-lat", String(lat));
mapDiv.setAttribute("data-lon", String(lon));
});
// If the pop-up text is specified.
if (popUpText != '') {
// Replace the %20 HTML Entity by an empty space.
// %20 is the result of texts being piped via URL.
popUpText = popUpText.replace(/%20/g, ' ');
// Bind the text to the current marker.
p1.bindPopup(popUpText);
}
// Check if MiniMap needs to be shown also.
if (minimap == "on") {
// Create a new Tile Layer for the MiniMap.
// Per documentation, do not reuse the existing Tile object
// to avoid rendering issues.
var mapTile2 = L.tileLayer.provider(mapTileProvider);
var miniMap = new L.Control.MiniMap(mapTile2).addTo(map);
}
// Add an event listener that will keep track the changes in the
// map's zoom levels and it will update the data attribute accordingly.
map.on('zoomend', function(e) {
mapDiv.setAttribute("data-zoom", map.getZoom());
});
</script>
</body>
</html>