OpenLayers深入浅出(1)----添加地图map

在页面上添加地图,首先要引入相应的OpenLayers的函数库,而后创建地图Map对象,为Map对象添加地图服务后,页面就可以显示地图了。代码如下:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <link rel="stylesheet" href="skin.css" type="text/css">
    <script src="../lib/OpenLayers.js"></script>
    <script type="text/javascript">
            var map = null;
        function init(){
            var options = {
                    controls:[new OpenLayers.Control.KeyboardDefaults]
            };
            
            map = new OpenLayers.Map("map",options);
            var wms = new OpenLayers.Layer.WMS("LayerName","http://vmap0.tiles.osgeo.org/wms/vmap0?",{layers:"basic"});
            map.addLayer(wms);
            map.zoomToMaxExtent();
        }
    
    </script>
</head>
<body οnlοad="init()">
    <div id="map" class="smallmap"></div>
</body>

</html>

代码中的div为显示地图的容器,地图要加载到哪个容器中所用方式为map = new OpenLayers.Map("map",options),这里的map为容器的id,options为地图参数。


map的定义方式有以下几种方式:

// create a map with default options in an element with the id "map1"
var map = new OpenLayers.Map("map1");

// create a map with non-default options in an element with id "map2"
var options = {
    maxExtent: new OpenLayers.Bounds(-200000, -200000, 200000, 200000),
    maxResolution: 156543,
    units: 'm',
    projection: "EPSG:41001"
};
var map = new OpenLayers.Map("map2", options);

// map with non-default options - same as above but with a single argument
var map = new OpenLayers.Map({
    div: "map_id",
    maxExtent: new OpenLayers.Bounds(-200000, -200000, 200000, 200000),
    maxResolution: 156543,
    units: 'm',
    projection: "EPSG:41001"
});

// create a map without a reference to a container - call render later
var map = new OpenLayers.Map({
    maxExtent: new OpenLayers.Bounds(-200000, -200000, 200000, 200000),
    maxResolution: 156543,
    units: 'm',
    projection: "EPSG:41001"
});


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值