Leaflet panTo (or setview) function?(传单 panTo(或 setview)功能?)
问题描述
我想创建一个 panTo 函数.当我单击链接时,地图会平移到坐标.但我不确定如何将值传递给函数.我开始为链接提供 Pointfield (pt) 值,如下所示:
I want to create a panTo -function. When I click a link the map pans to the coordinates. But im not sure how to pass the value to the function. I'm starting with giving the link the Pointfield (pt) value like this:
<a href="#" class="marker" value="{{ mymodel.pt }}">Link</a>
然后我一直在尝试这个:
Then I've been trying this:
$("#dialog").on("click", ".marker", function(e) {
e.preventDefault();
map.panTo($(this).attr("value"));
});
那没用.我认为这是一个范围问题,函数无法读取地图"变量,因为它不在初始地图脚本下.
That didn't work. I think it's a scope-issue where the function cant read the 'map' variable because it's not under the initial map script.
所以我的下一个想法是创建一个panTo"- 函数并将其放在初始地图脚本(这将是正确的范围)下,然后从点击事件中调用该函数.不确定它会起作用,但我想知道如何从链接中传递值"?
So my next idea is to create a "panTo"- function and place it under the inital map script (which would be the right scope) and call that function from the click event instead. Not sure it would work but Im wondering how to pass it the "value" from the link?
感谢您的回答!
推荐答案
您可以利用 HTML 中的 data
属性向地图添加导航.将纬度、经度和缩放保存到类似 data-position
的位置,然后在用户单击锚标记时使用一些 JavaScript 调用这些值.这里有一些代码来说明.
You can add navigation to your map by utilizing data
attributes in your HTML. Save the latitude,longitude and zoom to something like data-position
and then call those values with some JavaScript when the user clicks on the anchor tag. Here's some code to illustrate.
<div id="map">
<div id="map-navigation" class="map-navigation">
<a href="#" data-zoom="12" data-position="37.7733,-122.4367">
San Francisco
</a>
</div>
</div>
<script>
var map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Map data © <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>'
}).addTo(map);
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.setView(loc, z, {animation: true});
return false;
}
}
</script>
这篇关于传单 panTo(或 setview)功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:传单 panTo(或 setview)功能?
基础教程推荐
- 直接将值设置为滑块 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01