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

  1. <!DOCTYPE HTML>
  2. <title>OpenLayers Simplest Example</title>
  3. <div id="demoMap" style="height:250px"></div>
  4. <script src="OpenLayers.js"></script>
  5. <script>
  6. map = new OpenLayers.Map("demoMap");
  7. map.addLayer(new OpenLayers.Layer.OSM());
  8. map.zoomToMaxExtent();
  9. </script>
  1.  

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

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>OpenLayers Demo</title>
  5. <style type="text/css">
  6. html, body, #basicMap {
  7. width: 100%;
  8. height: 100%;
  9. margin: 0;
  10. }
  11. </style>
  12. <script src="OpenLayers.js"></script>
  13. <script>
  14. function init() {
  15. map = new OpenLayers.Map("basicMap");
  16. var mapnik = new OpenLayers.Layer.OSM();
  17. var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
  18. var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
  19. var position = new OpenLayers.LonLat(13.41,52.52).transform( fromProjection, toProjection);
  20. var zoom = 15;
  21.  
  22. map.addLayer(mapnik);
  23. map.setCenter(position, zoom );
  24. }
  25. </script>
  26. </head>
  27. <body onload="init();">
  28. <div id="basicMap"></div>
  29. </body>
  30. </html>

Extensions

Other tile sets

If you are deploying your own tile images (for example, with Mapnik), just use the layer definition below:

  1. var newLayer = new OpenLayers.Layer.OSM("New Layer", "URL_TO_TILES/${z}/${x}/${y}.png", {numZoomLevels: 19});
  2. 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).

  1. var fromProjection = new OpenLayers.Projection("EPSG:4326"); // transform from WGS 1984
  2. var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
  3. var extent = new OpenLayers.Bounds(-1.32,51.71,-1.18,51.80).transform(fromProjection,toProjection);
  4. function init() {
  5. var options = {
  6. restrictedExtent : extent,
  7. controls: [
  8. new OpenLayers.Control.Navigation(),
  9. new OpenLayers.Control.PanZoomBar(),
  10. new OpenLayers.Control.Attribution()
  11. ]
  12. };
  13. map = new OpenLayers.Map("Map", options);
  14. var newLayer = new OpenLayers.Layer.OSM(
  15. "New Layer",
  16. "URL_TO_TILES/${z}/${x}/${y}.png",
  17. {zoomOffset: 13, resolutions: [19.1092570678711,9.55462853393555,4.77731426696777,2.38865713348389]}
  18. );
  19. map.addLayer(newLayer);
  20. map.setCenter(new OpenLayers.LonLat(-1.25,51.75).transform(fromProjection,toProjection), 0); // 0=relative zoom level
  21. }

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

  1. div.olControlAttribution, div.olControlScaleLine {
  2. font-family: Verdana;
  3. font-size: 0.7em;
  4. bottom: 3px;
  5. }

Add Markers

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>OpenLayers Simplest Example</title>
  5. </head>
  6. <body>
  7. <div id="Map" style="height:250px"></div>
  8. <script src="OpenLayers.js"></script>
  9. <script>
  10. var lat = 47.35387;
  11. var lon = 8.43609;
  12. var zoom = 18;
  13.  
  14. var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
  15. var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
  16. var position = new OpenLayers.LonLat(lon, lat).transform( fromProjection, toProjection);
  17.  
  18. map = new OpenLayers.Map("Map");
  19. var mapnik = new OpenLayers.Layer.OSM();
  20. map.addLayer(mapnik);
  21.  
  22. var markers = new OpenLayers.Layer.Markers( "Markers" );
  23. map.addLayer(markers);
  24. markers.addMarker(new OpenLayers.Marker(position));
  25.  
  26. map.setCenter(position, zoom);
  27. </script>
  28. </body>
  29. </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:

  1. map.setCenter(
  2. new OpenLayers.LonLat(155000,465000) // Center of the map
  3. .transform(
  4. new OpenLayers.Projection("EPSG:28992"), // transform from new RD
  5. new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
  6. ),
  7. 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 添加OpenStreetMap(OSM)瓦片层示例的更多相关文章

  1. [原]OpenStreetMap数据瓦片服务性能篇

    上文说到如何利用node-mapnik架设OpenStreetMap瓦片服务,解决了有没有的问题.然而这个服务还是比较孱弱,主要表现在以下几个方面: 1. Node.js只能使用CPU的一个核,不能有 ...

  2. javascript里面的数组,json对象,动态添加,修改,删除示例

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  3. 改善用户体验之wordpress添加图片弹出层效果 (插件 FancyBox)

    下面说说在改善用户体验之wordpress添加图片弹出层效果.效果图如下:   像这篇文章如何在百度搜索结果中显示网站站点logo? 文章内有添加图片,没加插件之前用户点击图片时,是直接_black打 ...

  4. OpenLayers添加地图标记

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...

  5. openlayers 添加标记点击弹窗 定位图标闪烁

    环境vue3.0 ,地图为公用组件,将添加图标标记的方法放在公共地图的初始化方法里 同一时间弹窗和定位标识都只有一个,因而我把弹窗和定位标记的dom预先写好放到了页面 //矢量标注样式设置函数,设置i ...

  6. 033医疗项目-模块三:药品供应商目录模块——供货商药品目录t添加查询功能----------Dao层和Service层和Action层和调试

    什么叫做供货商药品目录t添加查询功能?就是说我们前面的博客里面不是说供货商登录后看到了自己供应的药品了么如下: 现在供货商想要往里面添加别的药品,那么这个药品的来源就是卫生局提供的那个Ypxx表(药品 ...

  7. OpenStreetMap(OSM) for developers

    This article from: http://wiki.openstreetmap.org/wiki/Develop OpenStreetMap isn't just open data - i ...

  8. OpenStreetMap(OSM) JMap Viewer(Java swing map)

    This article from:http://wiki.openstreetmap.org/wiki/JMapViewer JMapViewer is a java component which ...

  9. CNN结构:SPP-Net为CNNs添加空间尺度卷积-神经元层

    前几个CNN检测的框架要求网络的图像输入为固定长宽,而SPP-Net在CNN结构中添加了一个实现图像金字塔功能的卷积层SPP层,用于在网络中实现多尺度卷积,由此对应多尺度输入,以此应对图像的缩放变换和 ...

随机推荐

  1. Oracle EBS-SQL (MRP-5):重起MRP Manager.sql

    UPDATE fnd_concurrent_processes SET process_status_code = 'K' WHERE process_status_code not in ('K', ...

  2. idea编译工程时出现Error:java: 无效的目标发行版: 1.8

    见图,从上述可以看出工程用的jdk1.7,而idea编译时采用的是1.8版本(应该idea新版本内置的jre是1.8吧,默认编译采用1.8) 修改:如下图    http://blog.csdn.ne ...

  3. Tortoise-SVN 出现“unable to connect to a repository at url no element found”解决办法

    安装要SVN server服务器后,建立自己的Repositories,创建自己的项目文件夹 如,https://xxxxxxxxxx.com:8443/ 安装Tortoise-svn进行设置目标链接 ...

  4. hdu1588之经典矩阵乘法

    Gauss Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. Js节点属性与方法

    属性: Attributes     存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataType     返回此节点的数据类型 Definition     以D ...

  6. Font Awesome 4.0.3 提供了369个网页常用的矢量字体图标

    Font Awesome 为您提供了一套可缩放的字体矢量图标,可以快速自定义图标的大小,颜色,阴影,这些都可以通过CSS来实现,无需任何的JS代码哦. 一,主要特点如下: 1,一个字体,369个图标 ...

  7. (原)css 响应式媒体查询 模板

    @media only screen and (max-width:340px) { html,input{ font-size:80%; } } @media only screen and (ma ...

  8. js触摸屏案例

    js 手机端触发事事件.javascript手机端/移动端触发事件   处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件: 1 2 3 4 touchstart:  ...

  9. oracle中的B-TREE索引

    在字段值情况不同的条件下测试B-TREE索引效率 清空共享池和数据缓冲区alter system flush shared_pool;alter system flush buffer_cache; ...

  10. 使用 Struts 2 实现国际化

    struts2国际化(I18N) 国际化也叫I18N,是Internationalization的简称.Struts2国际化是建立在Java国际化基础上,只是Struts2框架对Java国际化进行了进 ...