File: maps.js

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (175 lines) | stat: -rw-r--r-- 5,972 bytes parent folder | download
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
// This file is part of Zoph.
//
// Zoph is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// Zoph is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Zoph; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

var zMaps=function() {
    var map;
    var markers;
    var layers;

    function createMap(div, provider) {
        var map = L.map('map').setView([0,0],1);
        L.Icon.Default.imagePath = basePath + "templates/default/images/";

        switch(provider) {
        case "mapbox":
            var url="https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}";
            var maxZoom=18;
            var attr='Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>';

            var street=L.tileLayer(url, {
                attribution: attr,
                maxZoom: maxZoom,
                id: 'mapbox/streets-v11',
                zoomOffset: -1,
                tileSize: 512,
                accessToken: mapbox_api_key
            }).addTo(map);

            var sat=L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}", {
                attribution: attr,
                maxZoom: 17,
                id: 'mapbox.satellite',
                detectRetina: true,
                accessToken: mapbox_api_key
            }).addTo(map);

            layers = { 
                "Satellite": sat,
                "Streetmap": street
            };
            break;
        case "osm":
            var url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
            var attr='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
            var street=L.tileLayer(url, {
                attribution: attr,
                detectRetina: true,
            }).addTo(map);
        }
        if(typeof layers != "undefined") {
            L.control.layers(layers).addTo(map);
        }
        this.map = map;
        this.markers = new L.featureGroup;
        this.markers.addTo(map);
    }

    function clickMap(e) {
        var latfield=document.getElementById('lat');
        var lonfield=document.getElementById('lon');
        var zoomfield=document.getElementById('mapzoom');
        var maptypefield=document.getElementById('maptype');

        latfield.value=e.latlng.lat;
        lonfield.value=e.latlng.lng;
        if(zoomfield) {
            zoomfield.value=e.target.getZoom();
        }
      
        zMaps.markers.remove();
        zMaps.markers = new L.featureGroup;
        zMaps.markers.addTo(zMaps.map);
        zMaps.createMarker(e.latlng.lat, e.latlng.lng);
    }

    function zoomUpdate(e) {
        var zoomfield=document.getElementById('mapzoom');
        if(zoomfield) {
            zoomfield.value=zMaps.map.getZoom();
        }
    }

    function createMarker(lat, lon, icon, title, infoBubble) {
        var marker=new L.marker( [lat, lon], {title: title});;
        if(icon) {
            marker.setIcon(new L.icon({ iconUrl: icon }));
        }
        zMaps.map.setView(new L.LatLng(lat,lon), zMaps.map.getZoom() || 14);

        if (infoBubble) {
            marker.bindPopup(infoBubble);
        }
        marker.addTo(zMaps.markers);
    }

    function createPolyline(points) {
        var polyline = L.polyline(points, {color : 'blue'}).addTo(zMaps.markers);
    }

    function setFieldUpdate() {
        // This makes sure that the map is updated when a user changes the
        // Lat and Lon fields manually.
        var latfield=document.getElementById("lat");
        var lonfield=document.getElementById("lon");
        var zoomfield=document.getElementById("mapzoom");

        latfield.onchange=zMaps.updateMap;
        lonfield.onchange=zMaps.updateMap;
        if(zoomfield) {
            zoomfield.onchange=zMaps.updateMap;
        }
    }

    function updateMap() {
        var latfield=document.getElementById("lat");
        var lonfield=document.getElementById("lon");
        var zoomfield=document.getElementById("mapzoom");
        var distance=document.getElementById("latlon_distance");
        var lat=latfield.value;
        var lon=lonfield.value;
        var zoomlevel=zMaps.map.getZoom();
        
        if(zoomfield) {
           zoomlevel=parseInt(zoomfield.value);
        }

        zMaps.markers.remove();
        zMaps.markers = new L.featureGroup;
        zMaps.markers.addTo(zMaps.map);
        zMaps.createMarker(lat, lon);
        createMarker(lat, lon,null,null,null);
        zMaps.setCenterAndZoom([lat, lon],zoomlevel);
    }

    function setCenterAndZoom(center, zoomlevel) {
        this.map.setView(center, zoomlevel);
    }

    function autoCenterAndZoom() {
        this.map.fitBounds(this.markers.getBounds());
    }

    function setUpdateHandlers() {
        this.map.on('click', zMaps.clickMap);
        this.map.on('zoomend', zMaps.zoomUpdate);
        this.map.on('moveend', zMaps.zoomUpdate);
        setFieldUpdate();
    }

    return {
        map:map,
        markers:markers,
        createMap:createMap,
        clickMap:clickMap,
        zoomUpdate:zoomUpdate,
        createMarker:createMarker,
        createPolyline:createPolyline,
        updateMap:updateMap,
        setCenterAndZoom:setCenterAndZoom,
        autoCenterAndZoom:autoCenterAndZoom,
        setUpdateHandlers:setUpdateHandlers

    };
}();