I’m new to leaflets and am trying to circle the map when clicked. I want conditions like the following:
- Circle moves from one point to another when clicked on the map (If the circle already exists and there is only one circle)
- Directly Zoom on where the circle is made
However I have the following problem:
- Circle increases
- When pressing another point, the circle disappears first and after that it will appear at the previous point and at the most recent point
- Don’t zoom in where the circle is made
Is there a solution to this problem?
JavaScript
x
38
38
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<title>Leaflet Polygons</title>
5
<link rel = "stylesheet" href = "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css"/>
6
<script src = "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
7
</head>
8
9
<body>
10
<div id = "map" style = "width: 900px; height: 580px"></div>
11
<script>
12
// Creating map options
13
var mapOptions = {
14
center: [16.506174, 80.648015],
15
zoom: 11
16
}
17
var groupCircle = L.featureGroup();
18
19
var map = new L.map('map', mapOptions); // Creating a map object
20
21
// Creating a Layer object
22
var layer = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
23
map.addLayer(layer); // Adding layer to the map
24
25
map.on("click", function(e){
26
if(map.hasLayer(groupCircle)){
27
map.removeLayer(groupCircle)
28
}else{
29
new L.Circle([e.latlng.lat, e.latlng.lng], 5000).addTo(groupCircle);
30
map.addLayer(groupCircle)
31
}
32
33
})
34
</script>
35
</body>
36
37
</html>
38
Advertisement
Answer
- The circle will increase because you delete only the featureGroup layer not the content of the featureGroup. Re-initiation can be done as follows:
JavaScript
1
3
1
map.removeLayer(groupCircle)
2
groupCircle = L.featureGroup();
3
- Can be added directly after re-initiation
JavaScript
1
12
12
1
if(map.hasLayer(groupCircle)){
2
//Start
3
map.removeLayer(groupCircle)
4
groupCircle = L.featureGroup();
5
new L.Circle([e.latlng.lat, e.latlng.lng], 5000).addTo(groupCircle);
6
map.addLayer(groupCircle)
7
//End
8
}else{
9
new L.Circle([e.latlng.lat, e.latlng.lng], 5000).addTo(groupCircle);
10
map.addLayer(groupCircle)
11
}
12
- Can add “setView” command:
JavaScript
1
2
1
map.setView([e.latlng.lat, e.latlng.lng], 11); //11 = zoom level
2
Here’s the full code:
JavaScript
1
44
44
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<title>Leaflet Polygons</title>
5
<link rel = "stylesheet" href = "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css"/>
6
<script src = "http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
7
</head>
8
9
<body>
10
<div id = "map" style = "width: 900px; height: 580px"></div>
11
<script>
12
// Creating map options
13
var mapOptions = {
14
center: [16.506174, 80.648015],
15
zoom: 11
16
}
17
var groupCircle = L.featureGroup();
18
19
var map = new L.map('map', mapOptions); // Creating a map object
20
21
// Creating a Layer object
22
var layer = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
23
map.addLayer(layer); // Adding layer to the map
24
25
map.on("click", function(e){
26
map.setView([e.latlng.lat, e.latlng.lng], 11); //11 = zoom level
27
if(map.hasLayer(groupCircle)){
28
//Start
29
map.removeLayer(groupCircle)
30
groupCircle = L.featureGroup();
31
new L.Circle([e.latlng.lat, e.latlng.lng], 5000).addTo(groupCircle);
32
map.addLayer(groupCircle)
33
//End
34
}else{
35
new L.Circle([e.latlng.lat, e.latlng.lng], 5000).addTo(groupCircle);
36
map.addLayer(groupCircle)
37
}
38
39
})
40
41
</script>
42
</body>
43
</html>
44