gpt4 book ai didi

maps - 有没有办法根据缩放级别更改图标图像? (传单.js)

转载 作者:行者123 更新时间:2023-12-04 05:41:39 25 4
gpt4 key购买 nike

我正在为 Web 应用程序制作一个区域绘制工具,我使用标记作为 anchor ,用户可以使用它来更改多边形的形状。

这是我到目前为止。 http://demos.nodeline.com/leaflet_development/

repo 在 https://github.com/SpencerCooley/Leaflet_development

$(document).ready(function(){

var map, cloudmade, sanAntonio, polygonPoints


map = new L.Map('map');

cloudmade = new L.TileLayer('http://{s}.tile.cloudmade.com/d4334cd6077140e3b92ccfae2b363070/997/256/{z}/{x}/{y}.png', {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>',
maxZoom: 18
});


sanAntonio = new L.LatLng(29.4238889, -98.4933333); // geographical point (longitude and latitude)


map.setView(sanAntonio, 13).addLayer(cloudmade);




polygonPoints = [];

var polygon = new L.Polygon(polygonPoints);
map.addLayer(polygon);

map.on('click', function(e) {


var marker = new L.Marker(e.latlng, {draggable:true});
polygonPoints.push(e.latlng);
var markerId = polygonPoints.length -1
map.addLayer(marker);
polygon.setLatLngs(polygonPoints);



marker.on('drag', function(){
var locationWhileDrag = marker.getLatLng();
$('#first_marker').val(locationWhileDrag);
polygonPoints.splice(markerId,1,locationWhileDrag);
polygon.setLatLngs(polygonPoints);
});



});







});

当用户放大到街道级别时,我只希望标记为正常大小。当您缩小正常大小的标记时,会完全淹没多边形。我查看了文档,但找不到任何关于此的信息。

我主要是在寻找建议/头脑 Storm 。我在想也许有一种方法可以检测您当前处于哪种缩放状态?如果是这样,我可以使用 if 语句来更改图标。

最佳答案

好的,所以我找到了一些方法并想出了这个:

//this sets up an icon to be replaced on redraw. 
var MyIcon = L.Icon.extend({
iconUrl: 'marker.png',
iconSize: new L.Point(10, 16),
shadowSize: new L.Point(10, 16),
iconAnchor: new L.Point(10, 16)
});

var icon = new MyIcon();

//When view resets use the smaller icon if zoom level is less than 13
map.on('viewreset', function(){
if(map.getZoom() < 13){
marker.setIcon(icon);
}
});

setIcon() 方法没有在文档中,我在谷歌论坛中找到了它并且有效。我制作了一个较小的图标,我基本上只是在缩放级别小于 13 时替换原始图标。我将要实现不同缩放级别的不同标记现在可以使标记“更远”效果。

这是修改后的代码。

$(document).ready(function(){

var map, cloudmade, sanAntonio, polygonPoints

map = new L.Map('map');

cloudmade = new L.TileLayer('http://{s}.tile.cloudmade.com/d4334cd6077140e3b92ccfae2b363070/997/256/{z}/{x}/{y}.png', {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>',
maxZoom: 18
});

sanAntonio = new L.LatLng(29.4238889, -98.4933333); // geographical point (longitude and latitude)

map.setView(sanAntonio, 13).addLayer(cloudmade);

polygonPoints = [];

var polygon = new L.Polygon(polygonPoints);
map.addLayer(polygon);

map.on('click', function(e) {
//this sets up an icon to be replaced when redraw.
var MyIcon = L.Icon.extend({
iconUrl: 'marker.png',
iconSize: new L.Point(10, 16),
shadowSize: new L.Point(10, 16),
iconAnchor: new L.Point(10, 16)
});

var icon = new MyIcon();
//this sets up an icon to be replaced when redraw.

var marker = new L.Marker(e.latlng, {draggable:true});
polygonPoints.push(e.latlng);
var markerId = polygonPoints.length -1
map.addLayer(marker);
polygon.setLatLngs(polygonPoints);

marker.on('drag', function(){
var locationWhileDrag = marker.getLatLng();
$('#first_marker').val(locationWhileDrag);
polygonPoints.splice(markerId,1,locationWhileDrag);
polygon.setLatLngs(polygonPoints);
});

//When view resets use the small icon if zoom level is less than 13
map.on('viewreset', function(){
if(map.getZoom() < 13){
marker.setIcon(icon);
}
});
});

});

这是演示:
http://demos.nodeline.com/leaflet_development/

关于maps - 有没有办法根据缩放级别更改图标图像? (传单.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10068500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com