gpt4 book ai didi

javascript - Leaflet:如何在自定义控件中设置panTo方法?

转载 作者:行者123 更新时间:2023-11-28 06:19:17 25 4
gpt4 key购买 nike

当您使用 Leaflet 的 panTo 方法单击缩略图时,我正在尝试平移和缩放到 map 的一部分。由于某种原因它不起作用。有人可以帮忙排除故障吗?这是我的代码和现场演示:

现场演示:jsfiddle

相关代码:

var jumpKabul = L.Control.extend({
options: { position: 'bottomleft' },
onAdd: function(map){
var container = L.DomUtil.create('div', 'test');
container.innerHTML = '<div id="map-navigation" class="map-navigation"><a href="#" data-zoom="12" data-position="34.51702396789498,69.11893844604492"><img src="https://placehold.it/150x150"></a></div>';
return container;
}
});

map.addControl(new jumpKabul());

document.getElementById('map-navigation').onclick = function(e) {
var pos = e.target.getAttribute('data-position');
var zoom = e.target.getAttribute('data-zoom');
if (pos && zoom) {
var loc = pos.split(',');
var z = parseInt(zoom);
map.panTo(loc, z, {animation: true});
return false;
}
}

最佳答案

事件目标不是您期望的那样,它是对 img 的引用,而不是包装 div 元素。您可以通过将 e.target 记录到控制台并检查其内容来发现这一点。如果您使用e.target.parentElement,那就没问题。下一个错误是您向 panTo 函数添加了一个不存在的缩放参数:

panTo( latlng, options? )

http://leafletjs.com/reference.html#map-panto

此外,您还没有充分发挥 L.Control 类的潜力。您仍然在类之外做繁琐的工作,附加事件处理程序和实际的事件处理等。尝试这样的事情,这样您就可以将所有逻辑包含在自定义控件类中(在代码片段中使用 Leaflet 但可以工作)也使用 Mapbox.js):

var map = new L.Map('leaflet', {
'center': [0, 0],
'zoom': 0,
'layers': [
new L.TileLayer('//{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
'attribution': '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'
})
]
});

L.Control.Navigation = L.Control.extend({

options: {
position: 'bottomleft'
},

onAdd: function (map) {

var container = L.DomUtil.create('div', 'map-navigation'),
link = L.DomUtil.create('a', 'map-navigation-link', container),
img = L.DomUtil.create('img', 'map-navigation-image', link);

link.href = '#';
// Unsure as why one would need the data attribs
// Since we could directly use the handler below
link.dataset.lat = 34.51702396789498;
link.dataset.lng = 69.11893844604492;
link.dataset.zoom = 12;

img.src = '//placehold.it/150x150';

L.DomEvent.addListener(link, 'click', function (e) {
// Using setView because it has zoom
map.setView([
link.dataset.lat,
link.dataset.lng
], link.dataset.zoom);
});

return container;
}
});

map.addControl(new L.Control.Navigation());
body {
margin: 0;
}

html, body, #leaflet {
height: 100%;
}
<!DOCTYPE html>
<html>

<head>
<title>Leaflet 0.7.7</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link type="text/css" rel="stylesheet" href="//cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
</head>

<body>
<div id="leaflet"></div>
<script type="application/javascript" src="//cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
</body>

</html>

关于javascript - Leaflet:如何在自定义控件中设置panTo方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35672613/

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