基于openlayer4实现地图联动效果


1.原理说明

基于openlayer4实现地图联动效果,就是创建多个地图容器,共用一个视图对象(ps:地图的视图关联着地图的extent事件,共用一个视图也就意味着所有地图容器同步一个事件,省去自己手动关联地图extent事件)。

2.代码说明

<!DOCTYPE html>
<html>
<head>
    <title>地图联动</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/css/ol.css" type="text/css">
    <script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
    <style>
        body{
            overflow: hidden;
        }
        html,body{
            width: 100%;
            height: 100%;
            position:absolute;
            box-sizing: border-box;
            padding: 0;
            margin: 0;
        }
        .map{
            width:50%;
            float: left;
            height: 100%;
        }
    </style>
</head>
<body>
<div id="map_1" class="map">
</div>
<div id="map_2" class="map ">
</div>
<script>
    var map_1 =  new ol.Map({
        target:"map_1",
        view:new ol.View({
            projection: "EPSG:4326",
            center:[115.7555794477557, 22.6070466884657],
            zoom:7
        }),
        layers:[
            new ol.layer.Tile({source:new ol.source.OSM()})
        ]
    });
    var map_2 = new ol.Map({
        target:"map_2",
        view:map_1.getView(),
        layers:[
            new ol.layer.Tile({
                source: new ol.source.XYZ({
                    url:'http://t3.tianditu.cn/DataServer?T=img_w&X={x}&Y={y}&L={z}'
                }),
                projection: "EPSG:4326",
                id:"影像"
            })
        ]
    });
</script>
</body>
</html>

转载自:https://blog.csdn.net/YH20090580118/article/details/79666455

You may also like...