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 添加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. NHbiernate 配置

    1.从网上下载NHibernate 4.0 文件.下载地址:https://sourceforge.net/projects/nhibernate/ 2.解压后把 NHibernate.dll文件引用 ...

  2. QT Creator 2.7.2 代码自动补全快捷键设置

    在QT Creater界面点[工具]再进[选项]找到[环境]下的[键盘]选项,搜索[CompleteThis]发现默认快捷键就是CTRL+SPACE,把它删除,然后添加自己想设置的快捷键(因为之前用e ...

  3. U盘读写速度测试

    1.ATTO Disk Benchmark    测U盘读写速度 ATTO Disk Benchmark 是一款简单易用的磁盘传输速率检测软件,可以用来检测硬盘.U盘.存储卡及其它可移动磁盘的读取及写 ...

  4. cf455A Boredom

    A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  5. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 分类: dp 2015-05-21 10:50 14人阅读 评论(0) 收藏

    Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并 ...

  6. 【LeetCode练习题】Merge Sorted Array

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

  7. oracle error info

    1,oracle jdbc HTTP Status 500 - Incorrect result size: expected 1, actual 0 2015-03-31 00:03:58,250 ...

  8. POJ 1811 Prime Test 素性测试 分解素因子

    题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的  素数与素性测试 素因子分解利用 ...

  9. GCC单独编译host/examples/ tx_waveforms.cpp

    1.编译 须要链接uhd库和boost_program_options库以及boost_thread库: g++ tx_waveforms.cpp -o a -luhd -lboost_program ...

  10. DG之主库、备库切换(物理备库)

    DG之主库.备库切换 一.开库与关库顺序 开库顺序 先启备库,再启主库(启动监听.打开告警日志) 关库顺序 先关主库,再关备库 二.主备库切换 1.操作过程一览 步骤1:启动备库.监听.告警: 步骤2 ...