圆形标记 - 基于缩放值的大小 Leaflet
Circle marker - size based on zoom values 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))
});
正在尝试找到一种根据实际缩放值动态调整圆形标记大小的方法。 我希望在使用缩放值计算大小时缩小时圆圈变小。尽量避免圆圈在缩小时发生太多碰撞但仍然有比例......我的想法是使用“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))
});