简体   繁体   English

Leaflet map 使图像叠加居中

[英]Leaflet map centering the image overlay

To start, I'm a bit new to leaflet (was using open layers before) and wanted to switch because the interface seemed a bit simple.首先,我对 leaflet (之前使用开放层)有点新,并且想切换,因为界面看起来有点简单。

Anyhow, my use case is quite simple, we want to use it only to display image maps that will have points on them.无论如何,我的用例非常简单,我们只想用它来显示将有点的图像地图。 Using the example here在此处使用示例

https://leafletjs.com/examples/crs-simple/crs-simple-example2.html https://leafletjs.com/examples/crs-simple/crs-simple-example2.html

We are able to get a map to display, add a map point, etc but where we are having an issue is trying to get the map to the center.我们能够得到一个 map 来显示,添加一个 map 点等,但是我们遇到的问题是试图让 map 到中心。 These maps will vary in size but no matter what we try the map always seems to be way off the screen when viewing OR way to zoomed in.这些地图的大小会有所不同,但无论我们尝试什么,map 在查看或放大时似乎总是远离屏幕。

I have a feeling this is due to the bound option but for the life of me, I can't seem to get a clear understanding of how this should work.我有一种感觉,这是由于绑定选项,但对于我的生活,我似乎无法清楚地了解它应该如何工作。

The sample code is very simple ( or not lol because I can't seem to get it to do what I want it to )示例代码非常简单(或者不是大声笑,因为我似乎无法让它做我想做的事)

HTML contains just one map div with styles of HTML 仅包含一个 map div 与 styles

#map {
    width: 600px;
    height: 600px;
}

The markup is标记是

var newMarker = "";
map = L.map('map', {
        crs: L.CRS.Simple,
         minZoom: -6,
         maxZoom:8,
         zoom:-6
    });

    var bounds = [[-1283,-2000], [2564,4000]];
    var image = L.imageOverlay(`path/to/image.jpg`, bounds).addTo(map);
    map.fitBounds(bounds);
    map.setZoom(-3);




map.on('click', addMarker);

As noted, the map renders but the image is always way off the screen where only part of the image shows and I have to pan it into view.如前所述,map 渲染但图像总是远离屏幕,只有部分图像显示,我必须将其平移到视图中。

I guess what I'm trying to figure out is how I can get this dang image to be centered and not super zoomed in, anyhow help in understanding how the bound should work would be great because I'm not grasping what the calculation should be to correctly center the image.我想我想弄清楚的是如何让这个 dang 图像居中而不是超级放大,无论如何有助于理解边界应该如何工作会很好,因为我没有掌握计算应该是什么正确居中图像。

Thanks!谢谢!

In case anyone else runs into this issue and much thanks to @IvanSanchez for taking the time to create the test code..万一其他人遇到这个问题,非常感谢@IvanSanchez 花时间创建测试代码..

Basically, what I was running into is the fact ( and I should have said this in the original post ) is that we call the map build on a function.基本上,我遇到的是一个事实(我应该在原帖中这么说)是我们在 function 上调用 map。 This worked but the map was way off center..这行得通,但 map 离中心太远了。

This lead to testing the code without the function and, it did work.. So this came back to how the map was being called, it was part of a 'wizard' where you select an item, then it moves to the next screen ( all on the same page ) we would build the map to a div that was yet to be displayed..这导致在没有 function 的情况下测试代码,并且它确实有效。所以这又回到了 map 的调用方式,它是“向导”的一部分,您在其中 Z99938282F040761859941E18F(然后它移动到下一个屏幕)都在同一页上)我们会将 map 构建到尚未显示的 div 中。

This meant it was alway off center.. This meant we needed to use这意味着它总是偏离中心。这意味着我们需要使用

map.invalidateSize(true)

This bit of code does resize the map, however it will not work if the map is in a 'hidden' state, so we had to refactor the code a bit to call the buildMap and invalidatesize after the map was visible This bit of code does resize the map, however it will not work if the map is in a 'hidden' state, so we had to refactor the code a bit to call the buildMap and invalidatesize after the map was visible

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM