如何使用leflet在地图事件中获取地图实例?
我想知道拖动地图时显示的地图四个角的纬度和经度。 因此,我编写了一个代码来获取四个角的经度和纬度,以及拖动事件发生的时间。
我测试这段代码。 代码捕获事件并触发事件处理程序。但是,当我运行map.getBound()时,解释器返回错误。错误消息如下
'Uncaught TypeError:无法读取未定义的属性(读取'getBounds')'
我很困惑。从错误消息来看,有可能地图实例尚未创建,但也有可能事件处理程序正在工作并且已创建。
出了什么问题以及如何在事件处理程序中获取地图的大小?
代码在这里,
let Map; // map instance
let X; // map position(longitude)
let Y; // map position(latitude)
let Z; // map zoom ratio
let ColorIndex; // item color index
let ColorCode; // item color code
/**
* @brief main program
* @details main program
* initialize then show contents
*/
function WebViewMain()
{
// initialize
OSM_CoreInit();
OSM_EventHandle();
}
function OSM_CoreInit()
{
this.X = 0.0; // map position(longitude)
this.Y = 0.0; // map position(latitude)
this.Z = 1; // map zoom ratio
this.Map = new L.Map('map').setView([this.X, this.Y], this.Z);
let tiles = L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1
});
tiles.addTo(this.Map);
}
function OSM_EventHandle()
{
this.Map.on( 'moveend', function(e){
alert( this.Map.getBounds());
});
this.Map.on( 'click', function(e){
let aa = e.latlng;
});
}
谢谢。
I want to know the latitude and longitude of the four corners of the displayed map when I drag the map.
So, I make a code to get the longitude and latitude of the four courners, at a drag event occurs timing.
I test this code.
The code catch the event and the event handler is fired. However, when I ran map.getBound (), the interpreter return an error.The error message is as follows
'Uncaught TypeError: Cannot read properties of undefined (reading 'getBounds')'
I'm confused. From the error message, it is possible that the map instance has not been created, but it is also possible that the event handler is working and has been created.
What's wrong and how can I get the size of the map in the event handler?
The code is here
let Map; // map instance
let X; // map position(longitude)
let Y; // map position(latitude)
let Z; // map zoom ratio
let ColorIndex; // item color index
let ColorCode; // item color code
/**
* @brief main program
* @details main program
* initialize then show contents
*/
function WebViewMain()
{
// initialize
OSM_CoreInit();
OSM_EventHandle();
}
function OSM_CoreInit()
{
this.X = 0.0; // map position(longitude)
this.Y = 0.0; // map position(latitude)
this.Z = 1; // map zoom ratio
this.Map = new L.Map('map').setView([this.X, this.Y], this.Z);
let tiles = L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1
});
tiles.addTo(this.Map);
}
function OSM_EventHandle()
{
this.Map.on( 'moveend', function(e){
alert( this.Map.getBounds());
});
this.Map.on( 'click', function(e){
let aa = e.latlng;
});
}
thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在函数中调用
this
时,它使用函数的上下文而不是您的类:您需要传递类的上下文。更改为:
或
或
When you call
this
in a function it uses the context of the function and not your class:You need to pass the context of the class. Change to:
or
or