3845 |
idir |
1 |
# Leaflet.GestureHandling
|
|
|
2 |
|
|
|
3 |
Version 1.2.1
|
|
|
4 |
|
|
|
5 |
Brings the basic functionality of [Google Maps Gesture Handling](https://developers.google.com/maps/documentation/javascript/examples/interaction-cooperative) into Leaflet.
|
|
|
6 |
|
|
|
7 |
Prevents users from getting trapped on the map when scrolling a long page.
|
|
|
8 |
|
|
|
9 |
**On Desktop**
|
|
|
10 |
|
|
|
11 |
![alt text](https://elmarquis.github.io/Leaflet.GestureHandling/examples/images/desktop.png "Desktop")
|
|
|
12 |
|
|
|
13 |
- The map ignores the mouse scroll wheel.
|
|
|
14 |
- The user is prompted to use ctrl+scroll to zoom the map.
|
|
|
15 |
|
|
|
16 |
**On Mobile / Touch devices**
|
|
|
17 |
|
|
|
18 |
![alt text](https://elmarquis.github.io/Leaflet.GestureHandling/examples/images/mobile.png "Mobile")
|
|
|
19 |
|
|
|
20 |
- The map ignores single fingered dragging.
|
|
|
21 |
- The user is prompted to use two fingers to pan the map.
|
|
|
22 |
|
|
|
23 |
## Demo
|
|
|
24 |
|
|
|
25 |
[View demo](https://elmarquis.github.io/Leaflet.GestureHandling/examples/)
|
|
|
26 |
|
|
|
27 |
## Install
|
|
|
28 |
|
|
|
29 |
The latest leaflet-gesture-handling can be downloaded from the dist folder.
|
|
|
30 |
|
|
|
31 |
## Usage
|
|
|
32 |
|
|
|
33 |
Include the css and js file after leaflet.js.
|
|
|
34 |
|
|
|
35 |
```html
|
|
|
36 |
<link rel="stylesheet" href="css/leaflet-gesture-handling.min.css" type="text/css">
|
|
|
37 |
<script src="js/leaflet-gesture-handling.min.js"></script>
|
|
|
38 |
```
|
|
|
39 |
|
|
|
40 |
Or load this directly from [UNPKG](https://unpkg.com):
|
|
|
41 |
|
|
|
42 |
```html
|
|
|
43 |
<link rel="stylesheet" href="//unpkg.com/leaflet-gesture-handling/dist/leaflet-gesture-handling.min.css" type="text/css">
|
|
|
44 |
<script src="//unpkg.com/leaflet-gesture-handling"></script>
|
|
|
45 |
```
|
|
|
46 |
|
|
|
47 |
Set **gestureHandling: true** in your map initialization script.
|
|
|
48 |
|
|
|
49 |
```js
|
|
|
50 |
var map = L.map("map", {
|
|
|
51 |
center: [-25.2702, 134.2798],
|
|
|
52 |
zoom: 3,
|
|
|
53 |
gestureHandling: true
|
|
|
54 |
});
|
|
|
55 |
```
|
|
|
56 |
|
|
|
57 |
Or you can enable and disable this dynamically on an existing map with `map.gestureHandling.enable()` and `map.gestureHandling.disable()`.
|
|
|
58 |
|
|
|
59 |
### Use as a module
|
|
|
60 |
|
|
|
61 |
If you are using a bundler such as Webpack or Parcel, you can load the library as a module. First install the module with:
|
|
|
62 |
|
|
|
63 |
```sh
|
|
|
64 |
npm install leaflet-gesture-handling
|
|
|
65 |
```
|
|
|
66 |
|
|
|
67 |
Then include the module and CSS in your source:
|
|
|
68 |
|
|
|
69 |
```js
|
|
|
70 |
import * as L from "leaflet";
|
|
|
71 |
import { GestureHandling } from "leaflet-gesture-handling";
|
|
|
72 |
|
|
|
73 |
import "leaflet/dist/leaflet.css";
|
|
|
74 |
import "leaflet-gesture-handling/dist/leaflet-gesture-handling.css";
|
|
|
75 |
```
|
|
|
76 |
|
|
|
77 |
Then attach it as a handler to the map:
|
|
|
78 |
|
|
|
79 |
```js
|
|
|
80 |
L.Map.addInitHook("addHandler", "gestureHandling", GestureHandling);
|
|
|
81 |
|
|
|
82 |
const map = L.map("map", {
|
|
|
83 |
center: [50.36, -4.747],
|
|
|
84 |
zoom: 3,
|
|
|
85 |
gestureHandling: true
|
|
|
86 |
});
|
|
|
87 |
```
|
|
|
88 |
|
|
|
89 |
## Multi Language and Custom Text
|
|
|
90 |
|
|
|
91 |
The plugin will auto detect a users language from the browser setting and show the appropriate translation. 52 languages are supported without you needing to do anything.
|
|
|
92 |
|
|
|
93 |
However if you wish to override this, you can set your own text by supplying **gestureHandlingOptions** and a **text** option as shown below. You must specify text for touch, scroll and scrollMac.
|
|
|
94 |
|
|
|
95 |
```js
|
|
|
96 |
var map = L.map("map", {
|
|
|
97 |
center: [-25.2702, 134.2798],
|
|
|
98 |
zoom: 3,
|
|
|
99 |
gestureHandling: true,
|
|
|
100 |
gestureHandlingOptions: {
|
|
|
101 |
text: {
|
|
|
102 |
touch: "Hey bro, use two fingers to move the map",
|
|
|
103 |
scroll: "Hey bro, use ctrl + scroll to zoom the map",
|
|
|
104 |
scrollMac: "Hey bro, use \u2318 + scroll to zoom the map"
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
});
|
|
|
108 |
```
|
|
|
109 |
|
|
|
110 |
*Note: Earlier versions used a property called gestureHandlingText for this which still works.
|
|
|
111 |
|
|
|
112 |
## Other Options
|
|
|
113 |
|
|
|
114 |
To pass in options use the property: **gestureHandlingOptions**.
|
|
|
115 |
|
|
|
116 |
- **duration** : (int) time in ms before the message should disappear. default: 1000 (1 sec)
|
|
|
117 |
|
|
|
118 |
```js
|
|
|
119 |
var map = L.map("map", {
|
|
|
120 |
center: [-25.2702, 134.2798],
|
|
|
121 |
zoom: 3,
|
|
|
122 |
gestureHandling: true,
|
|
|
123 |
gestureHandlingOptions: {
|
|
|
124 |
duration: 5000 //5 secs
|
|
|
125 |
}
|
|
|
126 |
});
|
|
|
127 |
```
|
|
|
128 |
|
|
|
129 |
## Useful info
|
|
|
130 |
|
|
|
131 |
GestureHandling disables the following map attributes.
|
|
|
132 |
|
|
|
133 |
- dragging
|
|
|
134 |
- tap
|
|
|
135 |
- scrollWheelZoom
|
|
|
136 |
|
|
|
137 |
They are temporarily enabled for the duration the user scrolls with ctrl+mousewheel or uses two fingers to pan the map on mobile.
|
|
|
138 |
|
|
|
139 |
## Compatibility with other plugins
|
|
|
140 |
|
|
|
141 |
I have added compatibility with [Leaflet-MiniMap](https://github.com/Norkart/Leaflet-MiniMap). It allows the user to still pan around the minimap with a single finger.
|
|
|
142 |
|
|
|
143 |
If there's any other plugins you'd like this to work with, let me know or submit a pull request.
|
|
|
144 |
|
|
|
145 |
## License
|
|
|
146 |
|
|
|
147 |
MIT
|