leaflet - 圆形标记 - 基于缩放值的大小传单
问题描述
试图找到一种通过实际缩放值动态确定圆形标记大小的方法。我希望通过使用缩放值计算大小来缩小圆圈时更小。尽量避免圆圈在缩小时碰撞太多但仍然有比例......我的想法是使用“getZoom”函数并使用该值来计算圆圈的大小。在下面的示例中将如何实现?我需要稍微玩一下才能找到正确的计算值,但现在无法正常工作,缺少一些东西......未定义是错误......
// Get zoom level
map.on('zoomend', function() {
var currentZoom = map.getZoom();
});
// Scale circle markers by using the zoom value
// you need to know what the min value is,
// calculated at runtime or prior
var minValue = 1;
// desired radius size of circles based on zoom levels
var minRadius = currentZoom/2;
function calcRadius(val) {
return 1.0083 * Math.pow(val/minValue,.5716) * minRadius;
}
............
............
编辑到这里************************************
// Scaling circle markers for best looking
// you need to know what the min value is,
// calculated at runtime or prior
var minValue = 1;
// minimum desired radius size of circles
//var minRadius = 4;
function calcRadius(val, zoom) {
return 1.0083 * Math.pow(val/minValue,0.5716) * (zoom / 2);
}
$.getJSON("../data/population/country.geojson", function(json) {
geoLayer = L.geoJson(json, {
pointToLayer: function(feature, latlng) {
var pop = (feature.properties.pop + feature.properties.pop2);
var circle;
map.on('zoomend', function() {
var currentZoom = map.getZoom();
circle.setRadius(calcRadius(circle._orgRadius,currentZoom))
});
// Filtrering
if (pop >= 1 && pop < 10){
circle = new L.circleMarker(latlng, {radius: 4, color: '#b30000', weight: 1.5}).addTo(map);
circle._orgRadius = circle.getRadius();
}
else if (pop >= 10 && pop < 25){
circle = new L.circleMarker(latlng, {radius: 4, color: '#d7301f', weight: 1.5}).addTo(map);
circle._orgRadius = circle.getRadius();
}
else if (pop >= 25 && pop < 50){
circle = new L.circleMarker(latlng, {radius: 4, color: '#ef6548', weight: 2}).addTo(map);
circle._orgRadius = circle.getRadius();
}
else if (pop >= 50 && pop < 100){
circle = new L.circleMarker(latlng, {radius: 4, color: '#fc8d59', weight: 2}).addTo(map);
circle._orgRadius = circle.getRadius();
}
else if (pop >= 100){
circle = new L.circleMarker(latlng, {radius: 4, color: '#fdbb84', weight: 2.5}).addTo(map);
circle._orgRadius = circle.getRadius();
}
else {
circle = null;
}
return circle;
},
解决方案
将您的代码更改为:
var circle = L.circleMarker([39.73, -104.99],{radius: 100}).addTo(map)
circle._orgRadius = circle.getRadius();
// Scale circle markers by using the zoom value
// you need to know what the min value is,
// calculated at runtime or prior
var minValue = 1;
function calcRadius(val, zoom) {
return 1.0083 * Math.pow(val/minValue,0.5716) * (zoom / 2);
}
map.on('zoomend', function() {
var currentZoom = map.getZoom();
// Recalc always with the original value
circle.setRadius(calcRadius(circle._orgRadius,currentZoom))
});
推荐阅读
- java - Java - 使用用户表单输入将图像上传到网站,将它们保存在 MySQL 数据库中,并在网站上显示图像
- javascript - 将值推送到状态时,我在 React Native 上遇到错误
- c# - 文字在 2 秒后持续闪烁。协程
- python - 根据列值对数据框的行进行样式设置
- android - 如何将 ListView.builder 和其他小部件与 Flutter 对齐
- python - Python MP3 播放器 tkinter 崩溃
- mongodb - 我如何创建一个 mongodb 查询,该查询使用唯一 id 计算值与其他时间戳的差异
- javascript - 按照它们在文档中出现的顺序对一组 HTMLElement 进行排序
- node.js - 从 REST API 获取图像并使用 Firebase Cloud Functions 将其上传到 Firebase Cloud Storage
- python-3.x - 打印设备输出的新行