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层,用于在网络中实现多尺度卷积,由此对应多尺度输入,以此应对图像的缩放变换和 ...
随机推荐
- ISP图像质量自动化测试方法
背景介绍 ISP(Image Signal Processor),即图像处理,主要作用是对前端图像传感器输出的信号做后期处理,主要功能有线性纠正.噪声去除.坏点去除.内插.白平衡.自动曝光控制等,依赖 ...
- 不是技术牛人,如何拿到国内IT巨头的Offer
原地址:http://blog.csdn.net/lsldd/article/details/13506263 不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些 ...
- linux之模拟简单登录的脚本
脚本如下: 运行结果:
- Matrix, Her, Transcendence
The Matrix showed us a traditional perspective of computation theory, which is a hierarchical world ...
- poj 2704 Pascal's Travels_记忆化搜索
一道简单但是题意蛋疼的题目 题意:给你个n*n的图,开始在左上角,要求走到右下角有多种走法,图上的数表示走几步,只能向右或向下走. #include<iostream> #include& ...
- alternaiate terms
操作系统就是能让您的计算机工作 的一系列基本程序和实用工具; 大多数的软件至少都要卖几百块钱,您们怎么愿意白白把它送给别人? 您真正应该问的问题是软件公司怎么可以要您花那么多钱买他们的软件.写软件和制 ...
- 【设计模式】学习笔记13:组合模式(Composite)
本文出自 http://blog.csdn.net/shuangde800 认识组合模式 上一篇中,我们可以用迭代器来实现遍历一个集合(数组,ArrayList, Vector, HashTabl ...
- mahout源码分析之Decision Forest 三部曲之二BuildForest(1)
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. BuildForest是在mahout-examples-0.7-job.jar包的org\apache ...
- Android_Layout_xml布局
本博文为子墨原创,转载请注明出处! http://blog.csdn.net/zimo2013/article/details/11840079 1.构建xml布局文件 使用android提供的xml ...
- DoNet开源项目-基于Amaze UI的点餐系统
帮朋友做的点餐系统,主要是为了让顾客在餐桌上,使用微信扫描二维码,就可以直接点菜,吃完使用微信付款. 系统演示地址,账户名和密码均为:admin.(请不要删除admin用户) GitHub Clone ...