基于js的高德地图无人机编队航行规划-一字型编队、随意设置点
横移飞
function oneword(n, lnglat, long, item, markers, marker, polyliness, text, index) {
if (n == 1) {
return;
}
for (var k = 0; k < n - 1; k++) {
var p = k + 2;
var lnglat0 = null;
if (position.length == 0) {
lnglat0 = getLngAndLat(lnglat[0], lnglat[1], 180, long * (k + 1));
} else {
lnglat0 = [keepSixDecimal(lnglat[0] - (position[index][0][0] - position[index][k + 1][0])),
keepSixDecimal(lnglat[1] - (position[index][0][1] - position[index][k + 1][1]))];
}
item.push(lnglat0);
var marker1 = null;
if (isno1) {
marker1 = new AMap.Marker({
position: new AMap.LngLat(lnglat0[0], lnglat0[1]),
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b" + p + ".png",
offset: new AMap.Pixel(-13, -30),
draggable: true,
extData: {
xid: k + 1,
},
map: map,
});
} else {
marker1 = new AMap.Marker({
position: new AMap.LngLat(lnglat0[0], lnglat0[1]),
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b" + p + ".png",
offset: new AMap.Pixel(-13, -30),
map: map,
});
}
var polyline1 = new AMap.Polyline({
path: [item[item.length-2], lnglat0],
strokeColor: "#336600",
strokeOpacity: 1,
strokeWeight: 1,
strokeStyle: "dashed",
strokeDasharray: [10, 5],
map: map,
});
markers.push(marker1);
polyliness.push(polyline1);
if (isno1) {
marker1.on('dragging', showInfoMO);
var text1 = new AMap.Text({
text: '',
style: {
'border-color': '#e1f5fe',
'font-size': '12px'
}
});
text.push(text1);
map.add(text1);
computeDis(markers[markers.length - 2], marker1, polyline1, text1);
}
}
}
第一个点拖动
function oneword1(n, lnglat, item, markers, marker, polyliness, text, index) {
if (n == 1) {
return;
}
var num = n - 1;
for (var k = 0; k < num; k++) {
var lnglat1 = [keepSixDecimal(lnglat[0] - (position[index][0][0] - position[index][k + 1][0])), keepSixDecimal(lnglat[1] - (position[index][0][1] - position[index][k + 1][1]))];
var marker1 = null;
var p = k + 2;
if (isno1) {
marker1 = new AMap.Marker({
position: new AMap.LngLat(lnglat1[0], lnglat1[1]),
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b" + p + ".png",
offset: new AMap.Pixel(-13, -30),
draggable: true,
extData: {
xid: k + 1,
},
});
} else {
marker1 = new AMap.Marker({
position: new AMap.LngLat(lnglat1[0], lnglat1[1]),
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b" + p + ".png",
offset: new AMap.Pixel(-13, -30),
});
}
map.add(marker1);
marker1.on('dragging', showInfoMO);
var polyline1 = new AMap.Polyline({
path: [lnglat, lnglat1],
strokeColor: "#336600",
strokeOpacity: 1,
strokeWeight: 1,
strokeStyle: "dashed",
strokeDasharray: [10, 5],
});
map.add(polyline1);
markers.push(marker1);
polyliness.push(polyline1);
if (isno1) {
var text1 = new AMap.Text({
text: '',
style: {
'border-color': '#e1f5fe',
'font-size': '12px'
}
});
text.push(text1);
map.add(text1);
computeDis(markers[markers.length - 2], marker1, polyline1, text1);
}
item.push(lnglat1);
}
}
设置第一个点不能移动
function oneword3(n, lnglat, marker) {
map.remove(markersList[0]);
marker = new AMap.Marker({
position: new AMap.LngLat(lnglat[0], lnglat[1]),
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b1.png",
offset: new AMap.Pixel(-13, -30),
});
map.add(marker);
markersList[0].splice(0, 1, marker);
if (n != 1) {
for (var k = 0; k < n-1; k++) {
var lnglat1 = [keepSixDecimal(lnglat[0] - (position[0][0][0] - position[0][k + 1][0])),
keepSixDecimal(lnglat[1] - (position[0][0][1] - position[0][k + 1][1]))];
var p = k + 2;
var marker1 = new AMap.Marker({
position: new AMap.LngLat(lnglat1[0], lnglat1[1]),
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b" + p + ".png",
offset: new AMap.Pixel(-13, -30),
});
map.add(marker1);
markersList[0].splice(k + 1, 1, marker1);
}
}
}
次marker移动事件
function showInfoMO(e) {
var num = e.target.getExtData().xid;
var lng = e.lnglat.getLng();
var lat = e.lnglat.getLat();
var lnglat1 = [lng, lat];
if (num < position[0].length - 1) {
map.remove(markersList[0][num]);
map.remove(polylines[0][num - 1]);
map.remove(polylines[0][num]);
map.remove(texts[0][num - 1]);
map.remove(texts[0][num]);
var p = num + 1;
var marker1 = new AMap.Marker({
position: new AMap.LngLat(lng, lat),
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b" + p + ".png",
offset: new AMap.Pixel(-13, -30),
draggable: true,
extData: {
xid: num,
},
});
marker1.on('dragging', showInfoMO);
map.add(marker1);
var polyline1 = new AMap.Polyline({
path: [position[0][num - 1], lnglat1],
strokeColor: "#336600",
strokeOpacity: 1,
strokeWeight: 1,
strokeStyle: "dashed",
strokeDasharray: [10, 5],
});
var polyline2 = new AMap.Polyline({
path: [lnglat1, position[0][num + 1]],
strokeColor: "#336600",
strokeOpacity: 1,
strokeWeight: 1,
strokeStyle: "dashed",
strokeDasharray: [10, 5],
});
map.add([polyline1, polyline2]);
var text1 = new AMap.Text({
text: '',
style: {
'border-color': '#e1f5fe',
'font-size': '12px'
}
});
var text2 = new AMap.Text({
text: '',
style: {
'border-color': '#e1f5fe',
'font-size': '12px'
}
});
map.add([text1, text2]);
computeDis(markersList[0][num - 1], marker1, polyline1, text1);
computeDis(marker1, markersList[0][num + 1], polyline2, text2);
position[0].splice(num, 1, lnglat1);
markersList[0].splice(num, 1, marker1);
polylines[0].splice(num - 1, 1, polyline1);
polylines[0].splice(num, 1, polyline2);
texts[0].splice(num - 1, 1, text1);
texts[0].splice(num, 1, text2);
} else {
map.remove(markersList[0][num]);
map.remove(polylines[0][polylines[0].length - 1]);
map.remove(texts[0][texts[0].length - 1]);
var p = num + 1;
var marker1 = new AMap.Marker({
position: new AMap.LngLat(lng, lat),
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b" + p + ".png",
offset: new AMap.Pixel(-13, -30),
draggable: true,
extData: {
xid: num,
},
});
marker1.on('dragging', showInfoMO);
map.add(marker1);
var polyline1 = new AMap.Polyline({
path: [position[0][position.length - 2], lnglat1],
strokeColor: "#336600",
strokeOpacity: 1,
strokeWeight: 1,
strokeStyle: "dashed",
strokeDasharray: [10, 5],
});
map.add(polyline1);
var text1 = new AMap.Text({
text: '',
style: {
'border-color': '#e1f5fe',
'font-size': '12px'
}
});
map.add(text1);
computeDis(markersList[0][markersList[0].length - 2], marker1, polyline1, text1);
position[0].splice(num, 1, lnglat1);
markersList[0].splice(num, 1, marker1);
polylines[0].splice(polylines[0].length - 1, 1, polyline1);
texts[0].splice(texts[0].length - 1, 1, text1);
}
}
工具类
function rad (d){
return d * Math.PI / 180.0;
};
function deg (x){
return x * 180 / Math.PI;
};
function getLngAndLat (lng,lat,brng,dist){
var a=6378137;
var b=6356752.3142;
var f=1/298.257223563;
var alpha1 = rad(brng);
var sinAlpha1 = Math.sin(alpha1);
var cosAlpha1 = Math.cos(alpha1);
var tanU1 = (1 - f) * Math.tan(rad(lat));
var cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1));
var sinU1 = tanU1 * cosU1;
var sigma1 = Math.atan2(tanU1, cosAlpha1);
var sinAlpha = cosU1 * sinAlpha1;
var cosSqAlpha = 1 - sinAlpha * sinAlpha;
var uSq = cosSqAlpha * (a * a - b * b) / (b * b);
var A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
var B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
var cos2SigmaM=0;
var sinSigma=0;
var cosSigma=0;
var sigma = dist / (b * A), sigmaP = 2 * Math.PI;
while (Math.abs(sigma - sigmaP) > 1e-12) {
cos2SigmaM = Math.cos(2 * sigma1 + sigma);
sinSigma = Math.sin(sigma);
cosSigma = Math.cos(sigma);
var deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)
- B / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
sigmaP = sigma;
sigma = dist / (b * A) + deltaSigma;
}
var tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
var lat2 = Math.atan2(sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1,
(1 - f) * Math.sqrt(sinAlpha * sinAlpha + tmp * tmp));
var lambda = Math.atan2(sinSigma * sinAlpha1, cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1);
var C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
var L = lambda - (1 - C) * f * sinAlpha
* (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
var revAz = Math.atan2(sinAlpha, -tmp);
var lngLatObj = [keepSixDecimal(lng+deg(L)),keepSixDecimal(deg(lat2))];
return lngLatObj;
}
function keepSixDecimal(num) {
var result = Math.round(num * 1000000) / 1000000;
return result;
};
function computeDis(m1, m2, line, text) {
var p1 = m1.getPosition();
var p2 = m2.getPosition();
var textPos = p1.divideBy(2).add(p2.divideBy(2));
var distance = Math.round(p1.distance(p2));
var path = [p1, p2];
line.setPath(path);
text.setText(distance + '米');
text.setPosition(textPos);
};
function bearing(start, end) {
let rad = Math.PI / 180,
lat1 = start[1] * rad,
lat2 = end[1] * rad,
lon1 = start[0] * rad,
lon2 = end[0] * rad;
const a = Math.sin(lon2 - lon1) * Math.cos(lat2);
const b = Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1);
let angle = radiansToDegrees(Math.atan2(a, b));
if (angle < 0) {
angle=360+angle;
}
return angle;
}
function radiansToDegrees(radians) {
const degrees = radians % (2 * Math.PI);
return degrees * 180 / Math.PI;
}
function getSinDeg(sin) {
var result = Math.asin(sin) / (Math.PI / 180);
result = Math.round(result);
return result;
}
function getDistance( lat1, lng1, lat2, lng2){
var radLat1 = lat1*Math.PI / 180.0;
var radLat2 = lat2*Math.PI / 180.0;
var a = radLat1 - radLat2;
var b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
s = s *6378.137 ;
s = Math.round(s * 10000) / 10000 * 1000;
return s;
}