-
Notifications
You must be signed in to change notification settings - Fork 0
/
13-Draggable original leaflet.html
163 lines (123 loc) · 3.81 KB
/
13-Draggable original leaflet.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Leaflet - Multiple geometries from one GeoJSON source</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<meta http-equiv="content-type" content="text/html"; charset="UTF-8"/>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
<!-- include leaflet.js library -->
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
<script src='https://cdn.rawgit.com/brandonxiang/leaflet.geoJsonFilter/develop/src/L.geoJsonFilter.js'></script>
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
}
</style>
</head>
<body>
<!--<style>
.coordinates {
background: rgba(0,0,0,0.5);
color: #fff;
position: absolute;
bottom: 10px;
left: 10px;
padding:5px 10px;
margin: 0;
font-size: 11px;
line-height: 18px;
border-radius: 3px;
display: none;
}
</style>-->
<div id='map'></div>
<pre id='coordinates' class='coordinates'></pre>
<script>
// Holds mousedown state for events. if this
// flag is active, we move the point on `mousemove`.
var isDragging;
// Is the cursor over a point? if this
// flag is active, we listen for a mousedown event.
var isCursorOverPoint;
//var coordinates = document.getElementById('coordinates');
var map = new L.Map('map');
L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> © <a href="http://cartodb.com/attributions">CartoDB</a>',
subdomains: 'abcd',
maxZoom: 19
}).addTo(map);
//var canvas = map.getCanvasContainer();
var geojson = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
}
}]
};
function pinsStyle(feature) {
return {
stroke: false,
radius: 10,
fillColor: "#3887be",
weight: 1,
opacity: 1,
fillOpacity: 1,
};
};
//crecion de la capa vacia
var LayerPins = L.geoJSON(false, {
pointToLayer: function(feature,latlng){
return L.circleMarker(latlng, pinsStyle(feature));
}
});
function mouseDown() {
if (!isCursorOverPoint) return;
isDragging = true;
// Mouse events
map.on('mousemove', onMove);
map.once('mouseup', onUp);
}
function onMove(e) {
if (!isDragging) return;
var coords = e.latlng;
// Update the Point feature in `geojson` coordinates
// and call setData to the source layer `point` on it.
geojson.features[0].geometry.coordinates = [coords.lng, coords.lat];
LayerPins.clearLayers();
LayerPins.addData(geojson);
LayerPins.addTo(map);
console.log(geojson);
}
function onUp(e) {
if (!isDragging) return;
var coords = e.lngLat;
// Print the coordinates of where the point had
// finished being dragged to on the map.
isDragging = false;
// Unbind mouse events
map.off('mousemove', onMove);
}
map.on('load', function() {
LayerPins.addData(geojson);
LayerPins.addTo(map);
LayerPins.on('mouseover', function(e){
isCursorOverPoint = true;
map.dragging.disable();
});
LayerPins.on('mouseout', function(e){
isCursorOverPoint = false;
map.dragging.enable();
});
LayerPins.on('mousedown', mouseDown
);
});
map.setView(new L.LatLng(0,0), 2);
</script>
</body>
</html>