Leaflet加载图片-imageoverlay
来源:互联网 发布:大数据平台 架构设计 编辑:程序博客网 时间:2024/06/08 05:47
原文地址 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 Overflow post 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.
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
thepixel coordinates of the south-westand north-east corners of the map intolat-long coordinates. We tell it todo that zoom level 3 (orgetMaxZoom()-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 of0
.Similarly, the top right corner has a y-coordinate of0
and an x-coordinate ofw
.
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).
- Leaflet加载图片-imageoverlay
- leaflet加载百度地图
- leaflet加载天地图
- leaflet延迟加载
- leaflet加载tms服务
- leaflet加载百度地图
- leaflet加载自定义投影瓦片
- leaflet本地加载arcgis切片
- Leaflet加载百度题图
- leaflet
- leaflet加载国家天地图服务
- leaflet加载百度地图(矫正篇)
- react-leaflet瓦片乱序/加载不全
- leaflet加载百度地图(矫正篇
- 使用react加载leaflet瓦片乱序或者加载不完全
- Leaflet+heatmap实现离线地图加载和热力图应用
- Leaflet+heatmap实现离线地图加载和热力图应用
- esri-leaflet入门教程(4)-加载各类图层
- meanshift算法学习(三):自己实现meanshift
- Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig的介绍和redi的整合使用
- linux常用命令
- vue项目打包到tomcat下运行
- 欢迎使用CSDN-markdown编辑器
- Leaflet加载图片-imageoverlay
- MySQL Query Profiler(性能分析工具)的使用方法
- JAVA NIO 概述及例子
- IOS实现3DTouch功能
- 【数据结构】【计算机视觉】并查集(disjoint set)结构介绍
- Androidstudio最简单的打成jar包的方法
- CSS盒子模型
- static作用(修饰函数、局部变量、全局变量)
- ant+jmeter+jenkins搭建持续性能测试,接口测试平台