1
2/* DOKUWIKI:include geohash.js */
3
4
5struct_gh_maps = new Array();
6struct_gh_markers = new Array();
7
8jQuery( document ).ready(function() {
9
10/*
11 * For each element of class 'struct_geohash_map'
12 * initializes de leaflet map and so on:
13 * */
14jQuery(".struct_geohash_map").each(function() {
15	// id is something like 'xxxx_map'
16	var id = jQuery(this).attr('id');
17	// the data for the leaflet library was writen to the data- tags:
18	var urlTMP = jQuery(this).attr('data-urlTMP');
19	var attribution = jQuery(this).attr('data-attribution');
20	var maxZoom = jQuery(this).attr('data-maxZoom');
21	var mapId = jQuery(this).attr('data-mapId');
22
23	// erase last 4 characters ('_map'):
24	var inputId = id.slice(0,-4);
25
26	// get the value of the input and decode de geohash:
27	var gh_default = jQuery("#"+inputId).attr('value');
28	var decoded = decodeGeoHash(gh_default);
29
30	// Initalizes the map:
31	var map = L.map(id).setView([decoded.latitude[2], decoded.longitude[2]], 13);
32
33	L.tileLayer(urlTMP, {
34		attribution: attribution,
35		maxZoom: maxZoom,
36		id: mapId,
37		tileSize: 512,
38		zoomOffset: -1,
39	}).addTo(map);
40
41	// Put the marker:
42	var marker = L.marker([decoded.latitude[2], decoded.longitude[2]]).addTo(map);
43
44	// If the edition is enabled, register a function to change the marker
45	if ( ! jQuery('#'+inputId).prop('disabled') ) {
46		map.on('click', function (e) { onMapClick(e, inputId); });
47	}
48
49	// Save the maps on a struct:
50	struct_gh_maps[inputId] = map;
51	struct_gh_markers[inputId] = marker;
52});
53
54});
55
56// On click, updates the input field and the marker position
57function onMapClick(e, id) {
58	struct_gh_markers[id].setLatLng(e.latlng);
59	mygh = encodeGeoHash(e.latlng.lat, e.latlng.lng);
60	jQuery("#"+id).attr('value',mygh);
61}
62