-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathleaflet.api.php
183 lines (174 loc) · 5.69 KB
/
leaflet.api.php
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
181
182
183
<?php
/**
* @file
* API documentation for the Leaflet module.
*/
/**
* Add map definitions to be used when rendering a map.
*
* leaflet_map_get_info() will grab every defined map, and the returned
* associative array is then passed to leaflet_render_map(), along with a
* collection of features.
*
* The settings array maps to the settings available to leaflet map object,
* https://leafletjs.com/reference.html
*
* Layers are the available base layers for the map and, if you enable the
* layer control, can be toggled on the map.
*
* @return array
* Associative array containing a complete leaflet map definition.
*/
function hook_leaflet_map_info() {
return array(
'OSM Mapnik' => array(
'label' => 'OSM Mapnik',
'description' => t('Leaflet default map.'),
'settings' => array(
'dragging' => TRUE,
'touchZoom' => TRUE,
'scrollWheelZoom' => TRUE,
'doubleClickZoom' => TRUE,
'zoomControl' => TRUE,
'attributionControl' => TRUE,
'trackResize' => TRUE,
'fadeAnimation' => TRUE,
'zoomAnimation' => TRUE,
'closePopupOnClick' => TRUE,
'layerControl' => TRUE,
// 'minZoom' => 10,
// 'maxZoom' => 15,
// 'zoom' => 15, // set the map zoom fixed to 15
),
'layers' => array(
'earth' => array(
'urlTemplate' => '//tile.openstreetmap.org/{z}/{x}/{y}.png',
'options' => array(
'attribution' => 'OSM Mapnik',
// The switchZoom controls require multiple layers, referencing one
// another as "switchLayer".
'switchZoomBelow' => 15,
'switchLayer' => 'satellite',
),
),
'satellite' => array(
'urlTemplate' => '//otile{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.png',
'options' => array(
'attribution' => 'OSM Mapnik',
'subdomains' => '1234',
'switchZoomAbove' => 15,
'switchLayer' => 'earth',
),
),
),
// Uncomment the lines below to use a custom icon.
// @code
// 'icon' => array(
// 'iconUrl' => '/sites/default/files/icon.png',
// 'iconSize' => array('x' => '20', 'y' => '40'),
// 'iconAnchor' => array('x' => '20', 'y' => '40'),
// 'popupAnchor' => array('x' => '-8', 'y' => '-32'),
// 'shadowUrl' => '/sites/default/files/icon-shadow.png',
// 'shadowSize' => array('x' => '25', 'y' => '27'),
// 'shadowAnchor' => array('x' => '0', 'y' => '27'),
// ),
// @endcode
),
);
}
/**
* Alter the map defined by another module in its hook_leaflet_map_info().
*
* @param array $map_info
* An array of map definitions, filtered for the ones available on the
* current page.
*
* @see hook_leaflet_map_info()
* @see leaflet_map_get_info()
*/
function hook_leaflet_map_info_alter(array &$map_info) {
// See examples for available data above in hook_leaflet_map_info().
}
/**
* Alters the js settings passed to the leaflet map.
*
* This hook is called when the leaflet map is being rendered and attaching the
* client side javascript settings.
*
* @param array $settings
* A javascript settings array used for building the leaflet map.
*
* @see leaflet_build_map()
*/
function hook_leaflet_map_prebuild_alter(array &$settings) {
$settings['mapId'] = 'my-map-id';
$settings['features']['icon'] = 'my-icon-url';
// Remove all controls. Available for all controls besides (dynamic) layer
// switcher or attribution.
$settings['mapControls']['ControlFullscreen'] = FALSE;
$settings['mapControls']['ControlScale'] = FALSE;
$settings['mapControls']['ControlZoomslider'] = FALSE;
$settings['mapControls']['ControlCoordinates'] = FALSE;
$settings['mapControls']['ControlViewCenter'] = FALSE;
// Also turn off default zoom buttons, which would appear, as the Zoomslider
// has been turned off above.
$settings['map']['settings']['zoomControl'] = FALSE;
}
/**
* Alter the build array before it is rendered.
*
* @param array $build
* Complete build array for this map.
*
* @see leaflet_build_map()
*/
function hook_leaflet_build_map_alter(array &$build) {
// Attach additional Javascript to maps.
$path = backdrop_get_path('module', 'my_module') . '/js/my-module.js';
$build['#attached']['js'][] = array(
'data' => $path,
'type' => 'file',
// Make sure this loads before leaflet.backdrop.js, so the event listener is
// ready, when the event is dispatched.
'group' => JS_LIBRARY,
'weight' => -1,
);
// File my-module.js:
/*
// @code
(function($) {
"use strict";
Backdrop.behaviors.myModule = {
attach: function (context, settings) {
// Add listener to custom leaflet event.
// This event is fired, as soon as the map is created.
$(document).on('leaflet.map', function (event, map, lMap) {
// Do whatever you need with the lMap...
});
}
};
})(jQuery);
// @endcode
*/
}
/**
* Customize the settings of a map.
*
* @param array $map
* Map info as defined in hook_leaflet_map_info().
* @param array $features
* Array of map items, like points or polygons.
* @param array $settings
* Field instance settings from field admin form.
* @param array $entity_wrapper
* Array containing entity_type and the entity (e.g. the node object).
*
* @see leaflet_field_formatter_view()
*/
function hook_leaflet_map_settings_alter(array $map, array $features, array &$settings, array $entity_wrapper) {
global $user;
if ($entity_wrapper['entity']->type == 'mytype' && !$user->uid) {
// Special customization per node type for anonymous only.
$settings['zoom']['maxZoom'] = 12;
}
}