百度地图经纬度和像素坐标互转

百度地图经纬度和像素坐标互转

1.经纬度转像素坐标
// 投影实例
const projection = map.getMapType().getProjection()

// 地图div宽高
const box = map.getSize()
const zoom = window.Math.pow(2, 18 - map.getZoom())

// 中心坐标 转 平面坐标
const center = projection.lngLatToPoint(map.getCenter())

// 测试坐标 转 平面坐标
// 这里根据自己的需求,设置point坐标
const point = projection.lngLatToPoint({ lng: 118.816832, lat: 31.981216 })

// 像素坐标
const x = Math.round((point.x - center.x) / zoom + box.width / 2)
const y = Math.round((center.y - point.y) / zoom + box.height / 2)

// 测试下 与pointToPixel转换是相同的
const pixel = map.pointToPixel({ lng: 118.816832, lat: 31.981216 })
console.log(pixel, x, y)
2.像素坐标转经纬度坐标
//投影实例
var projection = map.getMapType().getProjection();

// 地图div宽高
var box = map.getSize()
var zoom = window.Math.pow(2, 18 - map.getZoom());

// 中心坐标 转 平面坐标
var center = projection.lngLatToPoint(map.getCenter());

//元素左上角位置
var left = 100;
var top = 100;
var x = (left - box.width / 2) * zoom + center.x;
var y = center.y - (top - box.height / 2) * zoom;
var NW = map.getMapType().getProjection().pointToLngLat({
	x: x,
	y: y
});
//坐标点
var point = new BMap.Point(NW.lng, NW.lat);
//创建图像标注
map.addOverlay(new BMap.Marker(point));

转载于:https://my.oschina.net/jszhang/blog/3098870

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值