maptalks 坐标偏移问题

一、背景

因为国内对地理坐标有特殊的政策,所有电子地图必须对位置做偏移处理,这导致了从底层接口得到的经纬度坐标展示在地图上会有偏移。

二、前期准备

        1、引入gcoord

npm install gcoord --save

import gcoord from 'gcoord'
// 或者
import { transform, WGS84, GCJ02 } from 'gcoord'

        2、初始化地图参考之前文章或官方文档。

三、处理偏移

        1、整体图层偏移

                拿到初始化后的地图map,将新偏移过的baselayer图层重新加载至map中。

let tileLayer = new maptalks.TileLayer("baseLayer", {
    urlTemplate: url,
    offset: function (z) {
    //实时计算wgs84和gcj02瓦片的偏移量 将GCJ02坐标转换为WGS84坐标
    const center = map.getCenter();
    const c = gcoord.transform(center.toArray(), gcoord.GCJ02, gcoord.WGS84);
    const offset = map.coordToPoint(center, z).sub(map.coordToPoint(new maptalks.Coordinate(c), z));
        return offset._round().toArray();
    },
})
map.setBaseLayer(tileLayer)

        2、个别点偏移

// 将GCJ02坐标转换为WGS84坐标
const point = gcoord.transform([123, 45], gcoord.GCJ02, gcoord.WGS84);

maptalks - 官网

maptalks - 示例中心

goord-地理坐标系转换工具

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值