根据经纬度计算地图的缩放级别和中心点
本功能适用腾讯地图,百度地图,高德地图设置地图缩放级别和中心点
1.points是经纬度集合
2.centerW是中点的lat
3.centerJ是中点的lng
4.zoom是地图的锁房级别
getCenterPoint(points) {
//
本功能适用腾讯地图,百度地图,高德地图设置地图缩放级别和中心点
1.points是经纬度集合
2.centerW是中点的lat
3.centerJ是中点的lng
4.zoom是地图的锁房级别
getCenterPoint(points) {
//通过经纬度获取中心位置和缩放级别
var maxJ = points[0].lng;
var minJ = points[0].lng;
var maxW = points[0].lat;
var minW = points[0].lat;
var res;
for (var i = points.length - 1; i >= 0; i--) {
res = points[i];
if (res.lng > maxJ) maxJ = res.lng;
if (res.lng < minJ) minJ = res.lng;
if (res.lat > maxW) maxW = res.lat;
if (res.lat < minW) minW = res.lat;
}
if (maxJ == minJ && maxW == minW) return [maxJ, maxW, 9];
var diff = maxJ - minJ;
if (diff < maxW - minW) diff = maxW - minW;
diff = parseInt(10000 * diff) / 10000;
var centerJ = minJ * 1000000 + (1000000 * (maxJ - minJ)) / 2;
var centerW = minW * 1000000 + (1000000 * (maxW - minW)) / 2;
var zoom = this.getRoom(diff);
console.log(centerJ / 1000000, centerW / 1000000, zoom);
return [centerJ / 1000000, centerW / 1000000, zoom];
},
getRoom(diff) {
var room = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
var diffArr = new Array(360,180,90,45,22,11,5,2.5,1.25, 0.6,0.3,0.15,0.07,0.03,0);
for (var i = 0; i < diffArr.length; i++) {
if (diff - diffArr[i] >= 0) {
return room[i];
}
}
return 14;
},
//使用方法
var center = new tMap.LatLng(
this.getCenterPoint(this.lineList)[1],
this.getCenterPoint(this.lineList)[0]
);
//定义map变量,调用 TMap.Map() 构造函数创建地图
var map = new tMap.Map(document.getElementById("map"), {
center: center, //设置地图中心点坐标
zoom: this.getCenterPoint(this.lineList)[2], //设置地图缩放级别
});
版权声明
本站部分原创文章,部分文章整理自网络。如有转载的文章侵犯了您的版权,请联系站长删除处理。如果您有优质文章,欢迎发稿给我们!联系站长:
愿本站的内容能为您的学习、工作带来绵薄之力。
评论