define(function () {
/**
* 状态值
* @type {Boolean}
* @default false
* @private
*/
var status = {
initialized: false, //是否初始化
};
/**
* 模块数据 用于数据存储和外部调用
* @type {Object}
* 数据存放
*/
var modValue = {
title: "模拟飞行",
handler: null,
pointArr: [],
pointEntityArr: [],
lineEntity: null,
};
/**
*
*/
function init() {
if (!status.initialized) {
subscribe();
status.initialized = true;
// map3DViewer.flightSimulation({
// action: "add",
// callback: callback
// })
}
ONEMAP.C.publisher.publish({
modName: 'tool3DFlightSimulation'
}, 'tools:active');
};
/**
* 注册监听事件
*/
function subscribe() {
ONEMAP.C.publisher.subscribe(remove, 'tools:active');
ONEMAP.C.publisher.subscribe(removeAndClear, 'cleanMap');
ONEMAP.C.publisher.subscribe(removeEvent, 'change23D');
};
// 23D转换
function removeEvent() {
map3DViewer.flightSimulation({
action: "remove"
})
$('#toolFlightSimulationPanel').remove();
if ($(".tools-flightSimulation").hasClass('cur')) {
$(".tools-flightSimulation").removeClass('cur');
}
}
/**
* 3D通视 加载/移除 注册事件
*/
function remove(options) {
if (options.modName != 'tool3DFlightSimulation') {
// if (options.modName == 'cleanMap') {
// clear();
// }
removeAndClear();
$(".tools-flightSimulation").removeClass('cur');
} else {
if ($(".tools-flightSimulation").hasClass('cur')) {
$(".tools-flightSimulation").removeClass('cur');
removeAndClear();
} else {
$(".tools-flightSimulation").addClass('cur');
addPopup();
}
}
}
/**
* 移除 模拟飞行
*/
function removeAndClear() {
map3DViewer.flightSimulation({
action: "remove"
})
clearDrawEntity();
if (modValue.handler) {
modValue.handler.destroy()
modValue.handler = null;
}
$('#toolFlightSimulationPanel').remove();
};
function drawLine() {
var arr = modValue.pointArr.join(",").split(",")
if (!modValue.lineEntity) {
modValue.lineEntity = map3DViewer.map.entities.add({
polyline: {
positions: Cesium.Cartesian3.fromDegreesArray(arr),
width: 10.0,
material: new Cesium.PolylineGlowMaterialProperty({
color: Cesium.Color.CHARTREUSE.withAlpha(.5)
}),
clampToGround: true
}
});
} else {
modValue.lineEntity.polyline.positions.setValue(Cesium.Cartesian3.fromDegreesArray(arr))
}
}
function drawPoint(point) {
var entity = map3DViewer.map.entities.add({
position: Cesium.Cartesian3.fromDegrees(point.lng, point.lat),
point: {
pixelSize: 10,
heightReference: 2,
color: Cesium.Color.RED,
}
});
modValue.pointEntityArr.push(entity);
}
function clearDrawEntity() {
modValue.pointArr = []
if (modValue.lineEntity) {
map3DViewer.map.entities.remove(modValue.lineEntity)
modValue.lineEntity = null
}
if (modValue.pointEntityArr.length > 0) {
for (var i = 0; i < modValue.pointEntityArr.length; i++) {
map3DViewer.map.entities.remove(modValue.pointEntityArr[i])
}
modValue.pointEntityArr = [];
}
}
// 添加 弹窗
function addPopup() {
var html = '
';
$("body").append(html);
$("#toolFlightSimulationPanel .popup-ct").dragmove($('#toolFlightSimulationPanel'));
// 开始绘制/重新绘制
$('#toolFlightSimulationPanel .first_start,#toolFlightSimulationPanel .first_again').click(function () {
// 重新绘制时删除之前添加模拟飞行
if ($(this).hasClass("first_again")) {
map3DViewer.flightSimulation({
action: "remove"
})
}
$(this).siblings(".first_end").addClass("first_button_change")
$(this).removeClass("first_button_change")
// 重置第二步
resetSecondParam()
// 重置第三步
resetThirdParam()
// 清除已绘制的图形
clearDrawEntity()
modValue.handler = new Cesium.ScreenSpaceEventHandler(map3DViewer.map.scene.canvas);
modValue.handler.setInputAction(function (click) {
var pick1 = new Cesium.Cartesian2(click.position.x, click.position.y);
var cartesian = map3DViewer.map.scene.globe.pick(map3DViewer.map.camera.getPickRay(pick1), map3DViewer.map.scene);
if (cartesian) {
var cartographic = map3DViewer.map.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
var lat = Cesium.Math.toDegrees(cartographic.latitude);
var lng = Cesium.Math.toDegrees(cartographic.longitude);
var alt = cartographic.height;
var position = {
lat: lat,
lng: lng
}
modValue.pointArr.push([lng, lat])
drawPoint(position);
if (modValue.pointArr.length > 1) {
drawLine();
}
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
})
// 结束绘制
$('#toolFlightSimulationPanel .first_end').click(function () {
if (!modValue.lineEntity) {
clearDrawEntity()
$(this).siblings(".first_start").addClass("first_button_change")
$(this).removeClass("first_button_change")
} else {
$(this).siblings(".first_again").addClass("first_button_change")
$(this).removeClass("first_button_change")
if (modValue.handler) {
modValue.handler.destroy()
modValue.handler = null;
}
$('#toolFlightSimulationPanel .second').addClass("able")
}
})
$('#toolFlightSimulationPanel .simulate_start,#toolFlightSimulationPanel .simulate_restart').click(function () {
$(this).removeClass("second_button_change")
if ($(this).hasClass("simulate_restart")) {
$(this).addClass("second_button_change")
map3DViewer.flightSimulation({
action: "remove",
})
} else {
$(this).siblings(".simulate_restart").addClass("second_button_change")
}
var height = Number($('#toolFlightSimulationPanel .fly_height').val())
var speed = Number($('#toolFlightSimulationPanel .fly_speed').val())
modValue.speed = speed
var eye = $('#toolFlightSimulationPanel .second input[name="view"]:checked').val();
// 使第三步的视角与初始化视角相同
$('#toolFlightSimulationPanel .third input[value="' + eye + '"]').prop("checked", 'checked');
//重置第三步
resetThirdParam()
$('#toolFlightSimulationPanel .third').addClass("able")
$('#toolFlightSimulationPanel .third .third_button_change').removeClass("third_button_change")
$('#toolFlightSimulationPanel .third .fly_pause').addClass("third_button_change")
map3DViewer.flightSimulation({
action: "add",
eyeslook: eye,
height: height,
speed: speed,
lineCoor: modValue.pointArr,
lineColor: "#00ffcc",
outlineColor: "#FFFFFF",
modelUrl: "./data/airplane.glb",
callback: function () {}
})
})
// 模拟继续
$('#toolFlightSimulationPanel .fly_start').click(function () {
$('#toolFlightSimulationPanel .third .third_button_change').removeClass("third_button_change")
$('#toolFlightSimulationPanel .third .fly_pause').addClass("third_button_change")
map3DViewer.flightSimulation({
action: "resetCondition",
speed: modValue.speed
})
})
// 模拟暂停
$('#toolFlightSimulationPanel .fly_pause').click(function () {
$('#toolFlightSimulationPanel .third .third_button_change').removeClass("third_button_change")
$('#toolFlightSimulationPanel .third .fly_start').addClass("third_button_change")
map3DViewer.flightSimulation({
action: "resetCondition",
speed: 0
})
})
// 切换视角
$('#toolFlightSimulationPanel .third input[name="view_change"]').click(function () {
var eye = $(this).val();
// one two god
map3DViewer.flightSimulation({
action: "changeView",
eyeslook: eye
})
})
// 关闭
$('#toolFlightSimulationPanel .close').click(function () {
removeAndClear()
if ($(".tools-flightSimulation").hasClass('cur')) {
$(".tools-flightSimulation").removeClass('cur');
}
})
}
// 重置第二步
function resetSecondParam() {
$('#toolFlightSimulationPanel .second').removeClass("able")
$('#toolFlightSimulationPanel .second .second_button_change').removeClass("second_button_change")
$('#toolFlightSimulationPanel .second .simulate_start').addClass("second_button_change")
}
// 重置第三步
function resetThirdParam() {
$('#toolFlightSimulationPanel .third').removeClass("able")
$('#toolFlightSimulationPanel .third .third_button_change').removeClass("third_button_change")
$('#toolFlightSimulationPanel .third .fly_start').addClass("third_button_change")
}
// 判断是否为非负实数
function judgeNumber(docu, value) {
if (isNaN(Number(value))) {
docu.value = ""
} else {
if (/^\d+(\.\d+)?$/.test(value)) {
docu.value = value
}
}
}
return ONEMAP.M.tool3DFlightSimulation = {
init: init,
judgeNumber: judgeNumber
}
})