javascript - 根据滑块值增加圆的半径
问题描述
我现在有一张带有单个标记的地图。我在这个标记周围创建了一个圆形半径。这意味着给定标记位置的服务区域。
我在地图下方实现了一个滑块。现在,我正在尝试基于滑动此滑块来增加圆的半径。
我不知道如何开始。
有谁能够帮我?
var myloc = new L.LatLng(39.9042, 116.4074);
var map = L.map('map').setView(myloc, 15);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: 'pk.eyJ1Ijoic2FuaW5kcmEiLCJhIjoiY2pqam5qZzZnMHRycTNrbWR3ZGF4Mmd5eSJ9.ZRIVhopMmACa80OQ0yZN3g'
}).addTo(map);
var marker = L.marker(myloc).addTo(map);
var marker = L.marker(myloc).addTo(map);
var circle = L.circle(myloc, {
color: '#f03',
weight: 0.1,
fillColor: '#f03',
fillOpacity: 0.4,
radius: 100
}).addTo(map);
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value;
slider.oninput = function() {
output.innerHTML = this.value;
}
function clickCircle(e) {
var clickedCircle = e.target;
}
$('#myRange').on('change', function() {
clickCircle();
})
#map {
width: 400px;
height: 200px;
border: 1px solid #AAA;
}
.search-scope {
margin-top: 10px;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.3/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.3.3/dist/leaflet.js"></script>
<div id="map"></div>
<div class="search-scope">
<input type="range" min="100" max="1000" value="100" class="slider" id="myRange">
<p>Radius:
<span id="demo"></span>
</p>
</div>
解决方案
只需使用这样的.setRadius(radius)
方法circle.setRadius(this.value);
。当滑块改变时你需要这样做,所以在slider.oninput
函数中
请参阅下面的工作示例:
var myloc = new L.LatLng(39.9042, 116.4074);
var map = L.map('map').setView(myloc, 15);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: 'pk.eyJ1Ijoic2FuaW5kcmEiLCJhIjoiY2pqam5qZzZnMHRycTNrbWR3ZGF4Mmd5eSJ9.ZRIVhopMmACa80OQ0yZN3g'
}).addTo(map);
var marker = L.marker(myloc).addTo(map);
var marker = L.marker(myloc).addTo(map);
var circle = L.circle(myloc, {
color: '#f03',
weight: 0.1,
fillColor: '#f03',
fillOpacity: 0.4,
radius: 100
}).addTo(map);
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value;
slider.oninput = function() {
output.innerHTML = this.value;
circle.setRadius(this.value); // Sets the radius of the circle to be the value of the slider
}
function clickCircle(e) {
var clickedCircle = e.target;
}
/* Removed as was causing error */
/*$('#myRange').on('change', function() {
clickCircle();
})*/
#map {
width: 400px;
height: 200px;
border: 1px solid #AAA;
}
.search-scope {
margin-top: 10px;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.3/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.3.3/dist/leaflet.js"></script>
<div id="map"></div>
<div class="search-scope">
<input type="range" min="100" max="1000" value="100" class="slider" id="myRange">
<p>Radius:
<span id="demo"></span>
</p>
</div>
推荐阅读
- node.js - Azure 函数 context.log 在函数回调中不起作用
- java - 如何从 List 中获取元素
元素=新的 ArrayList<>();? - html - 如何将图像直接堆叠在 div 之上?
- json - 在 Angular 中难以实现分页功能
- css - Angular cdk拖放:拖放到其他容器后保持元素样式
- python - 如何在 Pytest 的收集时添加自定义 XML 属性?
- mysql - Delete rows to keep only top values in a sql table
- react-native - onEndReached 在数据加载完成之前触发
- ios - 在不覆盖自定义更改的情况下更新 GitHub iOS SDK
- unity3d - 如何让鼠标点击对象,而不是当它在被点击的按钮后面时?