openlayers加载百度地图二——ol5加载百度地图

本文介绍了一种针对百度地图瓦片坐标系特殊性进行地图显示纠偏的方法。通过自定义TileUrlFunction来适配百度地图的Y轴方向,解决了地图加载时的位置偏移问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于一般瓦片来说,origin在左上角,X轴从左至右递增,Y轴是从上往下递增(先计算左上角,然后计算右下角)
在这里插入图片描述
但是,百度瓦片Origin在[0,0],X轴从左至右递增,Y轴从下往上递增(从左下角到右上角)
在这里插入图片描述
所以,就要在tileUrlFunction上下点功夫了,废话不多说,直接撸代码吧:

<!--
 * @Author: yang xiunan
 * @Date: 2020-10-31 16:03:42
 * @LastEditTime: 2020-11-09 09:51:18
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ol6d:\myCode\ol5\src\components\HelloWorld.vue
-->
<template>
    <div class="box">
        <div id="map"></div>
    </div>
</template>

<script>
/* eslint-disable */
import Map from "ol/Map";
import View from "ol/View";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import { Fill, Icon, Stroke, Style, Text } from "ol/style";
import TileGrid from "ol/tilegrid/TileGrid";
import TileImage from "ol/source/TileImage";
import Tile from "ol/layer/Tile";
import Feature from "ol/Feature";
import Point from "ol/geom/Point";
export default {
    name: "HelloWorld",
    mounted() {
        let center = [108.946994, 34.261361];

        let resolutions = [];
        for (let i = 0; i < 19; i++) {
            resolutions[i] = Math.pow(2, 18 - i);
        }
        let tilegrid = new TileGrid({
            origin: [0, 0],
            resolutions: resolutions,
        });

        let baidu_source = new TileImage({
            projection: "EPSG:3857",
            tileGrid: tilegrid,
            tileUrlFunction: function (tileCoord, pixelRatio, proj) {
                if (!tileCoord) {
                    return "";
                }
                let z = tileCoord[0];
                let x = tileCoord[1];
                let y = tileCoord[2];

                if (x < 0) {
                    x = "M" + -x;
                }
                if (y < 0) {
                    y = "M" + -y;
                }

                return (
                    // "http://online3.map.bdimg.com/onlinelabel/?qt=tile&x=" +
                    // x +
                    // "&y=" +
                    // y +
                    // "&z=" +
                    // z +
                    // "&styles=pl&udt=20151021&scaler=1&p=1"

                    "https://api.map.baidu.com/customimage/tile?&x=" +
                    x +
                    "&y=" +
                    y +
                    "&z=" +
                    z +
                    "&udt=20201022&scale=1&styles=t%3Aland%7Ce%3Aall%7Cc%3A%2300121cff%2Ct%3Awater%7Ce%3Aall%7Cc%3A%2300445dff%2Ct%3Ahighway%7Ce%3Aall%7Cc%3A%2307313fff%2Ct%3Asubway%7Ce%3Aall%7Cv%3Aoff%2Ct%3Aarterial%7Ce%3Aall%7Cv%3Aon%7Cc%3A%23003242ff%2Ct%3Agreen%7Ce%3Aall%7Cv%3Aoff%2Ct%3Alabel%7Ce%3Al.t.s%7Cv%3Aon%7Cc%3A%2335bd8500%2Ct%3Arailway%7Ce%3Aall%7Cv%3Aoff%2Ct%3Alabel%7Ce%3Al.t.f%7Cv%3Aon%7Cc%3A%2335bd85ff%7Cw%3A1%2Ct%3Abuilding%7Ce%3Aall%7Cv%3Aon%7Cc%3A%231692beff%2Ct%3Amanmade%7Ce%3Aall%7Cv%3Aoff%2Ct%3Agreen%7Ce%3Aall%7Cv%3Aoff%2Ct%3Apoi%7Ce%3Al.t.f%7Cv%3Aon%7Cc%3A%2335bd85ff%2Ct%3Apoi%7Ce%3Al.t.s%7Cv%3Aon%7Cc%3A%2335bd8500%2Ct%3Apoi%7Ce%3Al.i%7Cv%3Aoff%2Ct%3Alocal%7Ce%3Aall%7Cv%3Aoff%2Ct%3Aroad%7Ce%3Al.i%7Cv%3Aoff"
                );
            },
        });

        let baidu_layer = new Tile({
            source: baidu_source,
        });

        let iconFeature = new Feature({
            geometry: new Point(center),
            name: "Null Island",
            population: 4000,
            rainfall: 500,
        });

        let iconStyle = new Style({
            image: new Icon({
                scale: 1,
                // anchorXUnits: 'fraction',
                // anchorYUnits: 'pixels',
                src: require("../assets/police.png"),
            }),
        });

        iconFeature.setStyle(iconStyle);

        let vectorSource = new VectorSource({
            features: [iconFeature],
        });

        let vectorLayer = new VectorLayer({
            source: vectorSource,
        });

        let map = new Map({
            target: "map",
            layers: [baidu_layer, vectorLayer],
            view: new View({
                center: center,
                zoom: 11,
                projection: "EPSG:4326",
            }),
        });
    },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#map {
    width: 100%;
    height: 100vh;
}
</style>

加载出来效果如下:
在这里插入图片描述
地图是加载出来了,但是偏移严重,我设置的中心点108.946994, 34.261361,是西安钟楼的,但是加载出来,位置都偏移到北三环以外了。

怎么解决偏移问题呢,我们下节再聊。

### 集成百度地图WMS服务到OpenLayers 要在 OpenLayers 中调用百度地图的 WMS 服务,需要了解几个关键点。首先,百度地图本身并不直接提供标准的 WMS 接口[^1],因此如果要实现此功能,则可能需要通过自定义方式或者第三方代理来模拟 WMS 请求。 以下是具体方法: #### 使用 TileLayer 和 ImageLayer 实现 WMS 效果 可以通过 `TileLayer` 或者 `ImageLayer` 来加载基于 URL 的瓦片或图像资源。下面是一个简单的代码示例展示如何配置这些层并将其添加至地图中。 ```javascript import 'ol/ol.css'; import { Map, View } from 'ol'; import TileLayer from 'ol/layer/Tile'; import XYZ from 'ol/source/XYZ'; import ImageWMS from 'ol/source/ImageWMS'; import ImageLayer from 'ol/layer/Image'; // 创建基础的地图视图 const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new XYZ({ url: 'https://online0.map.bdimg.com/tile/?qt=tile&x={x}&y={y}&z={z}' }) }), new ImageLayer({ source: new ImageWMS({ url: 'http://your-baidu-wms-proxy-url', // 替换为实际支持WMS协议的服务地址 params: {'LAYERS': 'baidumap-layer'}, serverType: 'geoserver' }) }) ], view: new View({ center: [104.11, 37.55], // 设置中心坐标 (经度, 纬度),需转换为投影坐标系 zoom: 8, projection: 'EPSG:4326' // 百度地图默认采用 EPSG:900913 投影,这里简化处理 }) }); ``` 注意,在上面的例子中,URL 地址 `'http://your-baidu-wms-proxy-url'` 是占位符,表示您需要找到一个能够解析百度地图数据并通过 WMS 协议返回结果的服务端接口。如果没有这样的服务存在,则可以考虑搭建自己的中间件服务器来进行适配[^2]。 另外需要注意的是,由于跨域请求限制以及版权保护等原因,直接访问某些商业地图服务商的数据可能会遇到困难。所以在开发前应确认目标平台是否允许此类操作,并遵循其 API 使用条款。 #### 关于投影变换 百度地图使用的是一种特殊的墨卡托投影(即 BD-09 坐标系),而大多数地理信息系统工具都倾向于使用全球统一的标准如 WGS84 或 EPSG:3857 。所以当尝试将两者结合起来时,往往还需要做额外的工作——即将地理位置从一种坐标系统映射到另一种上去。这一步骤通常借助专门库完成,比如 Proj.js 可用于执行复杂的几何运算和空间参照切换任务[^3]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值