如何删除leaflet已经加载的图片(imageOverlay)

2021-04-15 14:57发布

4条回答
杨晓春
2楼 · 2021-04-16 11:10

原文地址  http://kempe.net/blog/2014/06/14/leaflet-pan-zoom-image.html

leaflet使用imageoverlay加载图片,转载


Using leaflet.js to pan and zoom a big image


14 Jun 2014

I recently used leaflet.js to embed a large image in a webpage and allow users pan over it and zoom into it. There's aStack Overflowpost about how to do this for a single image, but the anwsers don't have enough detail to make it easy to understand and do.

The image I used was of a newspaper article. It's not big enough to needsegmentation, but it's too big to show on a page at full scale. I need to beable to read the article text. By using Leaflet I canzoom out to get anoverview of the layout, and zoom in andpan to read the text. Leaflettakes care of all the user interaction details for me.

Update 2016-04-05: See this gist for a full working example HTML file.

The result

Here's the end product.

+ -

Leaflet

and here's the code to make it work.

How it works

Let's walk through the code.

minZoom: 1,maxZoom: 4,center: [0, 0],zoom: 1,

We want to be able to zoom over 4 levels (1 to 4). Zoom level 3 is going to be the actual size (1 to 1) of the image.That means zoom level 4 will be twice as big, zoom level 2 will half as big,and zoom level 1 a quarter of the original size. We start by focusing on the center of the image, at zoom level 1.

  crs: L.CRS.Simple

This tells Leaflet to use a simple 1-1 mapping between screen pixels and its internal latitude-longitude coordinate system.In other words, our image is flat, not a globe that we're projecting onto a flat image.

Image sizes and coordinates

// dimensions of the imagevar w = 2000,h = 1500,url = '/images/newspaper-big.jpg';

This is the size of our image in pixels and its location.

// calculate the edges of the image, in coordinate spacevar southWest = map.unproject([0, h], map.getMaxZoom()-1);var northEast = map.unproject([w, 0], map.getMaxZoom()-1);var bounds = new L.LatLngBounds(southWest, northEast);

This is the fun bit. We want to tell Leaflet how to place the image on the map.So we ask it tounproject the pixel coordinates of the south-westand north-east corners of the map intolat-long coordinates. We tell it todo that zoom level 3 (or getMaxZoom()-1) since at that zoom level, the imagemust be actual size and fill the map.

In pixels, Leaflet considers the origin (0, 0) to be the top-left corner.So the bottom left corner has a y-coordinate ofh and an x-coordinate of 0.Similarly, the top right corner has a y-coordinate of0 and an x-coordinate of w.

The bounds variable now describes the lat-long coordinates of where on themap the image must be placed such that it is actual size at zoom level 3.

// add the image overlay, // so that it covers the entire mapL.imageOverlay(url, bounds).addTo(map);// tell leaflet that the map is exactly as big as the imagemap.setMaxBounds(bounds);

Finally, we add the image itself as an overlay to the map using the boundswe just calculated. We then tell Leaflet that the total size of the map isonly as the image, so the user can't drag past the edges.

Conclusion

The biggest step for me was wrapping my head around Leaflet's coordinatesystem and how and when to project from screen pixels into lat-long coordinatessuch that my zoom levels worked as expected.

I've found this to be a really powerful way of showing large images in a small pagewhen users need to be able to get both an overview and see the details. Leafletdoes all the hard work of providing a great-looking interface that the useris familiar with (provided they've used a mapping website).


我是大脸猫
3楼 · 2021-04-16 14:29

//添加

var overlay = L.imageOverlay(imageUrl, imageBounds).addTo(map);

//删除

map.removeLayer(overlay);


嘿呦嘿呦拔萝卜
4楼 · 2021-04-19 16:05

I recently used leaflet.js to embed a large image in a webpage and allow users pan over it and zoom into it. There's aStack Overflowpost about how to do this for a single image, but the anwsers don't have enough detail to make it easy to understand and do.

The image I used was of a newspaper article. It's not big enough to needsegmentation, but it's too big to show on a page at full scale. I need to beable to read the article text. By using Leaflet I canzoom out to get anoverview of the layout, and zoom in andpan to read the text. Leaflettakes care of all the user interaction details for me.

Update 2016-04-05: See this gist for a full working example HTML file.


灰机带翅膀
5楼 · 2021-08-20 16:10

image.png

这两行代码为加载图片代码和删除图片代码

相关问题推荐

  • 回答 120

    相对前几年来说,要高上不少了,毕竟入行的人也是越来越多了,基础的工作对应想要参与的人群基数越来越大,但是对于高端人才的需求还是很多,人才还是相对稀缺性的。所以,想要学web或者其他技术也一样,别等,别观望。web前端就业方向特别多包括web前端开发...

  • 回答 25

    相对定位和绝对定位是定位的两种表现形式,区别如下:一、主体不同1、相对定位:是设置为相对定位的元素框会偏移某个距离。2、绝对定位:absolute 脱离文档流,通过 top,bottom,left,right 定位。二、特点不同1、相对定位:在使用相对定位时,无论是否进行移...

  • 抓包是什么意思?2020-04-01 17:36
    回答 7
    已采纳

    抓包(packet capture)就是将网络传输发送与接收的数据包进行截获、重发、编辑、转存等操作,也用来检查网络安全。抓包也经常被用来进行数据截取等。抓包可以通过抓包工具来查看网络数据包内容。通过对抓获的数据包进行分析,可以得到有用的信息。目前流行的...

  • 回答 89

    常用的前端框架有Bootstrap框架、React框架、Vue框架、Angular框架、Foundation框架等等

  • 回答 65
    已采纳

    前端是目的就业前景非常不错的一个计算机技术,但是自学的话还是有一定难度的,网络上自学是碎片化的,同时互联网技术跟新换代快,自己的话比较吃力也学习不到最新的技术。

  • SSR 是什么意思?2020-03-20 18:56
    回答 6

    SSR就是一台服务器,可以利用 SSR 在远程的服务器上配置 SSR,使其能够成为 SSR 节点,这样本地电脑或者其它设备利用 SSR 节点实现 VPN 或者远程上网及游戏加速等方面。ShadowsocksR(简称 SSR)是 Shadowsocks 分支,在 Shadowsocks 的基础上增加了一些数据...

  • 回答 52
    已采纳

    计算机培训方向比较多,建议找适合自己的方向选择培训编程类:JAVA、WEB、Python、C/C++、C#等测试类:软件测试运维类:云计算、网络安全设计类:UI设计、3D建模等

  • 回答 11

    1、代码判断xAxis: {type: 'time',splitLine: {show: false},interval: 3600, // 设置x轴时间间隔axisLabel: {formatter: function(value, index) {return liangTools.unix2hm(value)}}},首先要把xAxis 显示类型设置成time,然后设置对应X轴......

  • 回答 8

    HTML5 + CSS + JavaScript 开发 跨平台重用代码 

  • 回答 4

    采用rem单位自动响应,并提供独有栅格化系统快速定义宽高、边距节省css代码量,同时总结各大型移动端网页,提供一套ui颜色搭配规范,尺寸规范,字体规范等。

  • 回答 10

    iView UI、ioni、SUI

  • 回答 6

     jQTouch 

  • 回答 4

    如果只是普通的移动端用vue react 或者dva 如果是要编译成小程序什么的或者混生 就用uni-app(对应vue语法)taro(对应react) 或者纯原生 这个没有限制的,自己怎么舒服怎么来

  • 回答 4

    因为可以运用在网页和小程序的开饭中,而且开源,用着便宜,企业都很喜欢

  • 回答 10

    一、Visual Studio Code下载地址:https://code.visualstudio.com/微软在2015年4月30日Build 开发者大会上正式宣布了 Visual Studio Code 项目:一个运行于 Mac OS X、Windows和 Linux 之上的,针对于编写现代 Web 和云应用的跨平台源代码编辑器。Visual Stud...

  • 回答 9

    jQuery自带淡入淡出效果 https://www.w3school.com.cn/jquery/jquery_fade.asp 看看这个 

没有解决我的问题,去提问