forked from ender74/hexo-tag-leaflet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps-template.html
91 lines (89 loc) · 2.89 KB
/
maps-template.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
<div id="<%- id %>" style="
height: <%- height %>;
width: <%- width %>;
margin: 0px;
padding: 0px
">
</div>
<script defer="defer">
window.hexoLmaps = window.hexoLmaps || { maps: {}};
window.hexoLmaps.maps['init<%- id %>'] = function init() {
var mymap = L.map('<%- id %>');
mymap.setView([<%- center.latitude %> , <%- center.longitude %>], <%- zoom %>);
var osmURL = '<%- baseLayer %>';
L.tileLayer(osmURL, {
attribution: '<%- attribution %>',
maxZoom: 18
}).addTo(mymap);
var geoJSON = '<%- geoJSON %>'
if (geoJSON) {
doLoadFile(geoJSON, function(data) {
var layer = L.geoJson(JSON.parse(data));
layer.addTo(mymap);
mymap.fitBounds(layer.getBounds());
});
}
<% _.each(markers, function(marker, i){%>
var icon<%- i %>
<% if (marker.icon && marker.icon.trim().length > 0) { %>
var iconOpts<%- i %> = {
iconUrl: '<%- marker.icon %>'
}
<% if (marker.iconSize) { %>
iconOpts<%- i %>.iconSize = <%- marker.iconSize %>
<% } %>
<% if (marker.iconAnchor) { %>
iconOpts<%- i %>.iconAnchor = <%- marker.iconAnchor %>
<% } %>
icon<%- i %> = L.icon(iconOpts<%- i %>)
<% } %>
var opts<%- i %> = {
'title': '<%- marker.name %>',
'zIndexOffset': <%- i %>
};
if (typeof(icon<%- i %>) !== 'undefined') {
opts<%- i %>.icon = icon<%- i %>
}
var marker<%- i %> = L.marker([<%- marker.latitude %>, <%- marker.longitude %>], opts<%- i %>).addTo(mymap);
<%}) %>
}
function doLoadFile(url, callback) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
callback(xhttp.responseText);
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
function makeMaps() {
for (var map in window.hexoLmaps.maps) {
console.log('map #', map);
window.hexoLmaps.maps[map]();
}
}
function doLoadScript(url, callback) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onreadystatechange = script.onload = function() {
if (!callback.done && (!script.readyState || /loaded|complete/.test(script.readyState))) {
callback.done = true;
callback();
}
};
document.querySelector('head').appendChild(script);
}
function loadScript() {
window.hexoLmaps.mapScripLoaded = true;
var css = document.createElement('link');
css.rel = 'stylesheet';
css.href = 'https://unpkg.com/[email protected]/dist/leaflet.css';
document.querySelector('head').appendChild(css);
doLoadScript('https://unpkg.com/[email protected]/dist/leaflet.js', makeMaps)
}
if (!window.hexoLmaps.mapScripLoaded) {
loadScript();
}
</script>