OpenLayers 添加OpenStreetMap(OSM)瓦片层示例
This article from:http://wiki.openstreetmap.org/wiki/OpenLayers_Simple_Example
Deploy an OpenStreetMap slippymap on my own website
This simple example may help if you are Deploying your own Slippy Map. This DHTML snippit will bring in the OpenLayers javascript library and use it to show an OSM map!
Note: OpenStreetMap is serving the tile images
Please note that tile images are coming from the OpenStreetMap servers. Although OSM are supporting this kind of usage at the moment, we offer no guarantees. There may be downtime (planned or unplanned), and tile URLs may change.
If you are expecting heavy user load, then you should discuss with everyone first (Contact). You should consider following the other instructions on creating your own tiles, or set up your own squid cache for tiles. This will reduce the dependency for you, and will ease bandwidth usage for the OSM servers.
Of course the images themselves (our maps) change over time too, not necessarily for the better.
Instructions
First, create a folder to work in. Download a stable release of OpenLayers from openlayers.org, and uncompress it. Copy the `OpenLayers.js` file and the `theme` directory to the base of the folder. Then, copy one of the following into a new HTML file, and view it in a browser.
The smallest example
<!DOCTYPE HTML>
<title>OpenLayers Simplest Example</title>
<div id="demoMap" style="height:250px"></div>
<script src="OpenLayers.js"></script>
<script>
map = new OpenLayers.Map("demoMap");
map.addLayer(new OpenLayers.Layer.OSM());
map.zoomToMaxExtent();
</script>
The code shows how you
- initialise a Map object with a DIVs id
- add a OpenStreetMap Layer
- force the tiles to show by calling zoomToMaxExtent, you could also call zoomToExtent, but for that you need a bounds object in the correct projection...
A little more extensive example
<!DOCTYPE HTML>
<html>
<head>
<title>OpenLayers Demo</title>
<style type="text/css">
html, body, #basicMap {
width: 100%;
height: 100%;
margin: 0;
}
</style>
<script src="OpenLayers.js"></script>
<script>
function init() {
map = new OpenLayers.Map("basicMap");
var mapnik = new OpenLayers.Layer.OSM();
var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
var position = new OpenLayers.LonLat(13.41,52.52).transform( fromProjection, toProjection);
var zoom = 15; map.addLayer(mapnik);
map.setCenter(position, zoom );
}
</script>
</head>
<body onload="init();">
<div id="basicMap"></div>
</body>
</html>
Extensions
Other tile sets
If you are deploying your own tile images (for example, with Mapnik), just use the layer definition below:
var newLayer = new OpenLayers.Layer.OSM("New Layer", "URL_TO_TILES/${z}/${x}/${y}.png", {numZoomLevels: 19});
map.addLayer(newLayer);
The addition of /${z}/${x}/${y}.png
URL template has been required since the 27th June 2009.
Change the url and numZoomLevels as appropriate.
Restricting the bounds & zoom levels
This restricts the map to showing the area around Oxford, and zoom levels 13-16. To add lower zooms, add new numbers in the resolutions array (each one is double the next).
var fromProjection = new OpenLayers.Projection("EPSG:4326"); // transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
var extent = new OpenLayers.Bounds(-1.32,51.71,-1.18,51.80).transform(fromProjection,toProjection);
function init() {
var options = {
restrictedExtent : extent,
controls: [
new OpenLayers.Control.Navigation(),
new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.Attribution()
]
};
map = new OpenLayers.Map("Map", options);
var newLayer = new OpenLayers.Layer.OSM(
"New Layer",
"URL_TO_TILES/${z}/${x}/${y}.png",
{zoomOffset: 13, resolutions: [19.1092570678711,9.55462853393555,4.77731426696777,2.38865713348389]}
);
map.addLayer(newLayer);
map.setCenter(new OpenLayers.LonLat(-1.25,51.75).transform(fromProjection,toProjection), 0); // 0=relative zoom level
}
Altering the location of the attribution text and scale line
You can override the location of the attribution text and scale line, and the font used, by adding the following lines in the style section
div.olControlAttribution, div.olControlScaleLine {
font-family: Verdana;
font-size: 0.7em;
bottom: 3px;
}
Add Markers
<!DOCTYPE HTML>
<html>
<head>
<title>OpenLayers Simplest Example</title>
</head>
<body>
<div id="Map" style="height:250px"></div>
<script src="OpenLayers.js"></script>
<script>
var lat = 47.35387;
var lon = 8.43609;
var zoom = 18; var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
var position = new OpenLayers.LonLat(lon, lat).transform( fromProjection, toProjection); map = new OpenLayers.Map("Map");
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik); var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
markers.addMarker(new OpenLayers.Marker(position)); map.setCenter(position, zoom);
</script>
</body>
</html>
Use Proj4js for other transformations
The example lets you use WGS84 coordinates to navigate in a sphericalMercator projected OSM map. If your coordinates are in a different projection, you can add Proj4js to perform reprojections.
Add the proj4js.js script from http://svn.osgeo.org/metacrs/proj4js/trunk/lib/proj4js-combined.js to your page (after the OpenLayers lib!)
Add your projection defintion (these are obtainable from the Proj4 project, you need the a record from \proj\nad\epsg
See http://svn.osgeo.org/metacrs/proj4js/trunk/lib/defs for examples
Example for EPSG:28992 (new RD)
Proj4js.defs["EPSG:28992"] = "+title=Amersfoort / RD New +proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs";
Then, you can use EPSG:28992 coordinates and this epsg code in the transformfunction instead of WGS84
like so:
map.setCenter(
new OpenLayers.LonLat(155000,465000) // Center of the map
.transform(
new OpenLayers.Projection("EPSG:28992"), // transform from new RD
new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
),
15); // Zoom level
Develop this example?
Feel free to edit this page with improvements.
This example was originally created by Harry Wood (and anyone else who edits this page). It is intentionally more basic, with only one layer defined, and no support for URL params (permalink) etc. So adding these features is not necessarily an improvement. In fact, if you have ideas for making this even more simple, that would be good.
Related
- OpenLayers Marker Add a simple static marker
- Openlayers POI layer example - Explains how to show POI markers with an overlay layer
- Full documentation of classes used is at the OpenLayers site or in the more up to date developer docu
- For further help and inspiration on using OpenLayers, you may wish to see the OpenLayers Examples.
- Another example of embedding a slippy OSM map with a GPX track on your website, based on the text above.
- Embed an OSM map with your track in webpage in the way similar to Google Maps (just provide a GPX or KML file).
OpenLayers 添加OpenStreetMap(OSM)瓦片层示例的更多相关文章
- [原]OpenStreetMap数据瓦片服务性能篇
上文说到如何利用node-mapnik架设OpenStreetMap瓦片服务,解决了有没有的问题.然而这个服务还是比较孱弱,主要表现在以下几个方面: 1. Node.js只能使用CPU的一个核,不能有 ...
- javascript里面的数组,json对象,动态添加,修改,删除示例
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 改善用户体验之wordpress添加图片弹出层效果 (插件 FancyBox)
下面说说在改善用户体验之wordpress添加图片弹出层效果.效果图如下: 像这篇文章如何在百度搜索结果中显示网站站点logo? 文章内有添加图片,没加插件之前用户点击图片时,是直接_black打 ...
- OpenLayers添加地图标记
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...
- openlayers 添加标记点击弹窗 定位图标闪烁
环境vue3.0 ,地图为公用组件,将添加图标标记的方法放在公共地图的初始化方法里 同一时间弹窗和定位标识都只有一个,因而我把弹窗和定位标记的dom预先写好放到了页面 //矢量标注样式设置函数,设置i ...
- 033医疗项目-模块三:药品供应商目录模块——供货商药品目录t添加查询功能----------Dao层和Service层和Action层和调试
什么叫做供货商药品目录t添加查询功能?就是说我们前面的博客里面不是说供货商登录后看到了自己供应的药品了么如下: 现在供货商想要往里面添加别的药品,那么这个药品的来源就是卫生局提供的那个Ypxx表(药品 ...
- OpenStreetMap(OSM) for developers
This article from: http://wiki.openstreetmap.org/wiki/Develop OpenStreetMap isn't just open data - i ...
- OpenStreetMap(OSM) JMap Viewer(Java swing map)
This article from:http://wiki.openstreetmap.org/wiki/JMapViewer JMapViewer is a java component which ...
- CNN结构:SPP-Net为CNNs添加空间尺度卷积-神经元层
前几个CNN检测的框架要求网络的图像输入为固定长宽,而SPP-Net在CNN结构中添加了一个实现图像金字塔功能的卷积层SPP层,用于在网络中实现多尺度卷积,由此对应多尺度输入,以此应对图像的缩放变换和 ...
随机推荐
- CodeForces 187A Permutations
反向思维,先求数组中不用处理的元素个数,再用n减去这个数,得到结果. #include <iostream> #include <cstring> #define maxn 2 ...
- 山寨QQ音乐的布局(二)终于把IOS6的UITableView拍扁了
IOS应用开发中UITableView的应用十分广泛,但是IOS7神一样的把UITableView拍扁了,这样一来IOS6的UITableView不干了,就吵着也要被拍扁,那好吧我今天就成全了你... ...
- Oracle EBS-SQL (PO-13):检查采购物料无一揽子协议价格.sql
Select msi.segment1 物料编码, msi.DESCRIPTION ...
- 杭电oj1236 排名
Tips:此题比较简单,最好将每一个学生的信息构建一个结构体,另外需要注意的是,若分数相同,排序按姓名排序,我看网上很多都是使用<algorithm>中的sort算法,只需重写cmp函数即 ...
- Windows下通过脚本快速修改IP地址
Windows下通过脚本快速修改IP地址 如果通过Windows的网络属性修改Ip/网关,真是太麻烦了. 经常要切换ip,所以我写了两个脚本: c:\办公室.bat netsh interface i ...
- android 中动画
详解Android动画之Frame Animation 写出动画效果的xml文件布局基本代码如下: <?xml version="1.0" encoding="ut ...
- 用于Lucene的各中文分词比较
对几种中文分析器,从分词准确性和效率两方面进行比较.分析器依次为:StandardAnalyzer.ChineseAnalyzer.CJKAnalyzer.IK_CAnalyzer.MIK_CAnal ...
- asp.net 树形控件 $.fn.zTree.init
在网页中通过jquery脚本来构筑树形控件将是一个不错的选择,比如有一个文本框,当鼠标点击的时候,像弹出一个下拉框一样弹出一个树形控件,这似乎是一个不错的控制.下面主要讲讲这种树形控件的实现.为了能使 ...
- 初学.NET小技巧(不断更新)
1.快速打出Console.WriteLine : 输入cw,然后按两下tab键. 2.创建一个函数快捷键:bool b = IsPrimeNumber(); 把光标放到函数名上,Shift+Al ...
- Nodejs随笔(三):全局对象之process
process是全局对象,在任何地方都可以访问,而且它是EventEmitter的一个实例(关于EventEmitter后面会提到). process对象对一些标准的输入输出流进行了封装,如stdin ...