I want to decide the Leaflet check box and delete the marker
As for the current code, if you check the box, it feels like the prefecture has a pin.
Every time I click it, the pin comes up and the shadow follows me.
I want to make it look like a pin will stand in Tokyo when Tokyo is turned on and a pin will disappear when it is turned off, but I can't imagine the code.
If you understand, please let me know.
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js">/script>
</head>
<body onload="init()">
<divid="mapcontainer" style="position:absolute;top:0;left:0;right:0;bottom:0;">/div>;
<div class="main" style="position: absolute;z-index:500;background-color:white;height:200px;">
<h2>State </h2>
<ul>
<li>
<div>
<input type="checkbox" onclick="tokyo()">Tokyo
</div>
</li>
<li>
<div>
<input type="checkbox" onclick="kanagawa()">Kanagawa
</div>
</li>
<li>
<div>
<input type="checkbox" onclick="chiba()">Chiba
</div>
</li>
</ul>
</div>
<script>
// Set the id of the div element to display the map
varmap = L.map('mapcontainer', {
zoomControl:false
});
// Open Street Map
varosm=L.tileLayer('http://tile.openstreetmap.jp/{z}/{x}/{y}.png', {
attribute: "<a href='http://osm.org/copyright' target='_blank'>OpenStreetMap</a>contributors"
});
function init() {
osm.addTo(map);
varpoint=[35.681236,139.767125];
map.setView (mpoint, 10);
}
function Tokyo() {
L. marker ([35.658182, 139.702043]).bindPopup("Tokyo").addTo(map);
}
function kanagawa() {
L. marker ([35.491354, 139.284143]).bindPopup("Kanagawa").addTo(map);
}
function chiba(){
L.marker([35.335416, 140.183252]).bindPopup("Chiba").addTo(map);
}
</script>
</body>
</html>
Removing the Layer (from which Marker is inherited) in removeFrom is easy.
To do this, you need to specify the layer corresponding to the checkbox, but there are several ways to do this.
The Chiba method is flexible because it checks everything, but I don't recommend it as a response this time because of the heavy load.
Personally, I recommend the Kanagawa method.
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js">/script>
</head>
<body onload="init()">
<divid="mapcontainer" style="position:absolute;top:0;left:0;right:0;bottom:0;">/div>;
<div class="main" style="position: absolute;z-index:500;background-color:white;height:200px;">
<h2>State </h2>
<ul>
<li>
<div>
<input type="checkbox" name="checkTokyo"onclick="tokyo()">Tokyo
</div>
</li>
<li>
<div>
<input type="checkbox" onchange="kanagawa(this.checked)">Kanagawa
</div>
</li>
<li>
<div>
<input type="checkbox" onclick="chiba()">Chiba
</div>
</li>
</ul>
</div>
<script>
// Set the id of the div element to display the map
varmap = L.map('mapcontainer', {
zoomControl:false
});
// Open Street Map
varosm=L.tileLayer('http://tile.openstreetmap.jp/{z}/{x}/{y}.png', {
attribute: "<a href='http://osm.org/copyright' target='_blank'>OpenStreetMap</a>contributors"
});
function init() {
osm.addTo(map);
varpoint=[35.681236,139.767125];
map.setView (mpoint, 10);
}
// Declare Maker in advance
let markerTokyo=L.marker([35.658182,139.702043]);
function Tokyo() {
// Obtain check box status and control display
let check = document.getElementsByName("checkTokyo")[0];
if(check.checked){
markerTokyo.bindPopup("Tokyo").addTo(map);
} else{
markerTokyo.removeFrom(map);
}
}
let markerKanagawa=null;
function kanagawa(value) {
// Turn on the check box to pass the check box selection status
if(value){
// The return value of addTo is Layer itself, so store it in a variable.
markerKanagawa=L.marker([35.491354,139.284143]).bindPopup("Kanagawa").addTo(map);
} else{
markerKanagawa.removeFrom(map);
}
}
function chiba(){
// Take out all Markers and check if the corresponding coordinates are placed
let found = false;
map.eachLayer(function(layer){
if (layer instance of L. Marker) {
if(layer.getLatLng().equals(L.latLng(35.335416, 140.183252)){
found = true;
layer.removeFrom(map);
}
}
});
// If not, add
if(!found){
L.marker([35.335416, 140.183252]).bindPopup("Chiba").addTo(map);
}
}
</script>
</body>
</html>
567 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
884 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
577 PHP ssh2_scp_send fails to send files as intended
606 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.