forked from FriendsOfProcessWire/FieldtypeLeafletMapMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkupLeafletMap.js
120 lines (97 loc) · 3.23 KB
/
MarkupLeafletMap.js
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
/**
* ProcessWire Leaflet Map Markup (JS)
*
* Renders maps for the FieldtypeLeafletMapMarker module
*
* ProcessWire 2.x
* Port of Google Map Markup by Ryan Cramer
* Copyright (C) 2013 by Ryan Cramer
* Copyright (C) 2015 by Mats Neander
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
* Javascript Usage:
* =================
* var map = new jsMarkupLeafletMap();
* map.setOption('any-leaflet-maps-option', 'value');
* map.setOption('zoom', 12); // example
*
* // init(container ID, latitude, longitude):
* map.init('#map-div', 26.0936823, -77.5332796);
*
* // addMarker(latitude, longitude, optional URL, optional URL to icon file):
* map.addMarker(26.0936823, -77.5332796, 'en.wikipedia.org/wiki/Castaway_Cay', '');
* map.addMarker(...you may have as many of these as you want...);
*
* // optionally fit the map to the bounds of the markers you added
* map.fitToMarkers();
*
*/
function jsMarkupLeafletMap() {
this.map = null;
this.markers = [];
this.numMarkers = 0;
this.options = {
zoom: 10,
center: null,
};
this._currentURL = '';
this.init = function(mapID, lat, lng, provider) {
if(lat != 0) this.map = L.map(mapID, {center: [lat, lng], zoom: this.options.zoom, scrollWheelZoom: this.options.scrollWheelZoom,} );
L.tileLayer.provider(provider).addTo(this.map);
}
this.setOption = function(key, value) {
this.options[key] = value;
}
var markers = new L.MarkerClusterGroup({polygonOptions: {color: 'teal', weight: 1, opacity: .39, lineJoin: 'round'}});
var marker = '';
this.addMarker = function(lat, lng, url, title, extra) {
if(lat == 0.0) return;
var latLng = L.latLng(lat, lng);
var markerOptions = {
linkURL: '',
title: title
};
marker = L.marker(latLng, markerOptions);
markers.addLayer(marker);
this.map.addLayer(markers);
this.markers[this.numMarkers] = marker;
this.numMarkers++;
if(url.length > 0) {
marker.linkURL = url;
if (extra.length > 0) {
extra = '<br />' + extra;
}
marker.bindPopup("<b><a href='" + marker.linkURL + "'>" + title + "</a></b>" + extra);
}
}
this.addMarkerIcon = function(icon, lat, lng, url, title, extra) {
if(lat == 0.0) return;
var latLng = L.latLng(lat, lng);
marker = L.marker(latLng, {icon: icon, linkURL: '', title: title});
markers.addLayer(marker);
this.map.addLayer(markers);
this.markers[this.numMarkers] = marker;
this.numMarkers++;
if(url.length > 0) {
marker.linkURL = url;
if (extra.length > 0) {
extra = '<br />' + extra;
}
marker.bindPopup("<b><a href='" + marker.linkURL + "'>" + title + "</a></b>" + extra);
}
}
this.fitToMarkers = function() {
var map = this.map;
var mg = [];
for(var i = 0; i < this.numMarkers; i++) {
mg.push(this.markers[i].getLatLng());
}
map.fitBounds(mg);
}
this.setMaxZoom = function() {
var map = this.map;
map.setZoom(this.options['zoom']);
}
}