原理就是不断改变地图中心点,改变相机角度方向,明白这一点,其他地图引擎譬如cesium都可效仿,本人就是通过cesium的漫游实现四维图新的漫游,唯一不足的是转弯的时候不能丝滑转向,尝试过应该是四维图新引擎的问题
export default function flyView (map, roadLine,nextIndex){
let index = 1;
let ding;
let currentCenter;
if(nextIndex){
index = nextIndex;
}
const flyTime = 12000;
function setExtentTime(time) {
const startTime = new Date().getTime();
const stopTime = startTime + time;
return {
stopTime,
startTime
};
}
function bearings(startLat, startLng, destLat, destLng) {
const y = Math.sin(destLng - startLng) * Math.cos(destLat);
const x = Math.cos(startLat) * Math.sin(destLat) - Math.sin(startLat) * Math.cos(destLat) * Math.cos(destLng - startLng);
const brng = Math.atan2(y, x);
const brngDgr = brng * 180 / Math.PI;
return brngDgr;
}
function flyExtent() {
if(index>=roadLine.length-1){
index = 1;
}
const time = setExtentTime(flyTime);
const bearing = Math.abs(bearings(roadLine[index - 1].lat, roadLine[index - 1].lng, roadLine[index].lat, roadLine[index].lng));
map.setCenter([roadLine[index - 1].lng,roadLine[index - 1].lat])
map.setBearing(Math.abs(bearing));
const loop = function () {
const delTime = new Date().getTime() - time.startTime;
const stepLng = (roadLine[index].lng - roadLine[index - 1].lng) / flyTime * delTime;
const stepLat = (roadLine[index].lat - roadLine[index - 1].lat) / flyTime * delTime;
const currentBearing = map.getBearing();
if (delTime > 0) {
const endPosition = [roadLine[index - 1].lng + stepLng,
roadLine[index - 1].lat + stepLat];
map.setCenter([endPosition[0], endPosition[1]]);
}
};
ding = setInterval(() => {
loop();
if (new Date().getTime() - time.stopTime >= 0) {
console.log(111);
clearInterval(ding);
index = ++index >= roadLine.length ? 0 : index;
if (index != 0) {
console.log(index);
flyExtent();
}
}
}, 100);
function stopFlyExtent() {
clearInterval(ding);
currentCenter = map.getCenter();
}
function startFlyExtent() {
roadLine[index-1].lng = currentCenter.lng
roadLine[index-1].lat = currentCenter.lat
flyExtent(index);
}
return {stopFlyExtent,startFlyExtent,index};
}
const control = flyExtent();
return control;
}
const roadLine = [
{
lng: 118.30918373160092,
lat: 33.8934430266404
},
{
lng: 118.317598598913,
lat: 33.89589007385073
},
{
lng: 118.32260700135133,
lat: 33.897263166704974
},
{
lng: 118.32527174444655,
lat: 33.897387332495924
},
]
使用
this.control = flyView(map, roadLine);
this.control.startFlyExtent();
this.control.stopFlyExtent();