openlayers加载百度地图四——ol6加载百度地图

本文介绍将OpenLayers从5升级到6后遇到的地图瓦片错位问题及解决方案。通过修改tileUrlFunction中的y坐标为相反数来适配OpenLayers 6加载瓦片的新方式。然而,此方法并未完全解决问题,例如标记位置出现偏差。

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

当把openlayers从5升级到6时,发现加载百度地图就发生瓦片错位的情况。
在这里插入图片描述
ol6加载瓦片的方式是从中心点依次向右下角递增,二ol5是从中心点依次向右上角递增,所以,需要把tileUrlFunction中的y坐标改为相反数即可:

<template>
    <div class="box">
        <div id="container"></div>
    </div>
</template>

<script>
/* eslint-disable */
import {Map, View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import TileImage from 'ol/source/TileImage';
import XYZ from 'ol/source/XYZ';
import TileGrid from 'ol/tilegrid/TileGrid';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import {Icon, Style,Stroke,Fill} from 'ol/style';

export default {
	name: "home",
	components:{

	},
	data(){
		return {

        }
	},
	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";
            }
		});

		this.map = new Map({
			target:'container',
			layers: [ 
				new TileLayer({
					source:baidu_source,
				}),
			],
			view: new View({
				center: center,
				zoom:10,
				maxZoom:18,
				projection: "EPSG:4326"
			}) 
		})
	
        this.vectorlayer = this.addLayer("vector");
		this.addPoints();
    },
	methods:{
		// 加载图层
		addLayer(name){
			let layer = new VectorLayer({
				source:new VectorSource(),
				className:name,
			})
			this.map.addLayer(layer);
			return layer;
		},
		
		// 加载点
		addPoints(){
			let point = [108.94238,34.26097]; //西安钟楼
            let feature = new Feature({
                geometry:new Point(point),
            })
            feature.setStyle(new Style({
                    image: new Icon({
                        anchor: [0.5, 0.5],
                        src: require(`../assets/images/fire.png`),
                        crossOrigin: '',
                        scale:[0.5,0.5],
                        anchorXUnits: 'fraction',
                        anchorYUnits: 'pixels',
                    }),
                })
            )
			this.vectorlayer.getSource().addFeature(feature);
		},
	},
};
</script>

<style scoped lang="less">
.box {
    width: 100%;
	height: 100%;
	position: relative;
	#container{
		width:100%;
		height: 100%;
		/deep/ .ol-control{
			display:none;
		}
	}
}
</style>

在这里插入图片描述
这样瓦片倒是不错位了,但是钟楼的坐标却显示到北三环以外去了,并且地图缩小时,钟楼的坐标更离谱的显示到国外去了。
在这里插入图片描述
那么,我们是否可以和上一节一样,用projzh解决偏移呢?我试了一下,发现还是无法解决。

抱歉,这个问题我暂时无法解决,哪位朋友如果有好的方法,我们一起讨论

### 集成百度地图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]。 --- ###
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值