define(['html!templates/menu/baseCalc/calcDistance',
'css!styles/tools/toolPublicPopup',
'css!styles/menu/baseCalc/calcDistance',
],
function (tplLayout) {
/**
* 模块状态,用于存储模块的状态 例如:收起,关闭
* @type {Object}
*/
var status = {
initialized: false//是否初始化
};
/**
* 模块数据 用于数据存储和外部调用
* @type {Object}
* 数据存放
*/
var modValue = {
data: {},
handler: null,
startPointId: "",
stopPointId: "",
startPointStyle: {
color: "#2ecc71",
fontFillColor: "#fff000",
labelOffset: -15,
},
stopPointStyle: {
color: "#3498db",
fontFillColor: "#fff000",
labelOffset: 15,
}
};
/**
* 初始化并订阅事件
* @return {[type]} [description]
*/
function open(data) {
if (!status.initialized) {
setLayout();
bindEvent();
subscribe();
status.initialized = true;
} else {
// 重置
reset();
}
modValue.data = data;
$("#calcDistanceModal .title").text(data.label);
ONEMAP.C.publisher.publish({
modName: 'calcDistance'
}, 'baseCalc:active');
};
function setLayout() {
$('body').append(tplLayout);
//拖拽
$("#calcDistanceModal .popup-ct").dragmove($('#calcDistanceModal'));
};
/**
* 事件绑定
*/
function bindEvent() {
$("#calcDistanceModal .close").bind('click', function () {
close();
});
$("#calcDistanceModal .btn-default.sure").bind('click', function () {
execute()
});
$("#calcDistanceModal .btn-default.clickStart").bind('click', function () {
getMapClickEvent(function (lonlat) {
clearSingleMarker(modValue.startPointId, "startPointId")
$("#calcDistanceForm .startPointLon").val(lonlat.lon);
$("#calcDistanceForm .startPointLat").val(lonlat.lat);
modValue.startPointId = addMarker(lonlat.lon, lonlat.lat, modValue.startPointStyle, "起点")
})
});
$("#calcDistanceModal .btn-default.clickStop").bind('click', function () {
getMapClickEvent(function (lonlat) {
clearSingleMarker(modValue.stopPointId, "stopPointId")
$("#calcDistanceForm .stopPointLon").val(lonlat.lon);
$("#calcDistanceForm .stopPointLat").val(lonlat.lat);
modValue.stopPointId = addMarker(lonlat.lon, lonlat.lat, modValue.stopPointStyle, "终点")
})
});
$("#calcDistanceModal .btn-default.cleanStart").bind('click', function () {
$("#calcDistanceForm .startPointLon").val("");
$("#calcDistanceForm .startPointLat").val("");
clearSingleMarker(modValue.startPointId, "startPointId")
});
$("#calcDistanceModal .btn-default.clearStop").bind('click', function () {
$("#calcDistanceForm .stopPointLon").val("");
$("#calcDistanceForm .stopPointLat").val("");
clearSingleMarker(modValue.stopPointId, "stopPointId")
});
//回车执行
$('#calcDistanceInput, #jumpToLatInput').bind('keydown', function (e) {
if (e.keyCode === 13) {
execute();
}
});
};
function getMapClickEvent(callback) {
if (modValue.handler != null) return;
modValue.handler = new Cesium.ScreenSpaceEventHandler(map3DViewer.map.canvas);
// 监听鼠标点击事件
modValue.handler.setInputAction(function (click) {
// 使用pick函数获取点击位置的实际位置
// var cartesian = map3DViewer.map.scene.pickPosition(click.position);
// var cartesian = map3DViewer.map.camera.pickEllipsoid(click.position);
let cartesian = map3DViewer.map.scene.globe.pick(map3DViewer.map.camera.getPickRay(click.position), map3DViewer.map.scene);
if (Cesium.defined(cartesian)) {
// 将笛卡尔坐标转换为经纬度坐标
var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
var longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(6);
var latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(6);
// var heightString = cartographic.height.toFixed(2);
// console.log('经度:' + longitudeString + ',纬度:' + latitudeString + ',高度:' + heightString)
}
if (isNaN(longitudeString) || isNaN(latitudeString)) {
alert("当前获取坐标有误,请重新获取")
removeMapClickEvent();
throw "当前获取坐标有误,请重新获取";
}
callback({
lon: Number(longitudeString),
lat: Number(latitudeString)
})
removeMapClickEvent();
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
modValue.handler.setInputAction(function (click) {
removeMapClickEvent();
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}
function removeMapClickEvent() {
if (modValue.handler) {
modValue.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
modValue.handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
modValue.handler = null;
}
}
/**
* 注册监听
* @type {Function}
*/
function subscribe() {
ONEMAP.C.publisher.subscribe(remove, 'baseCalc:active');
};
/**
* 关闭模块
* @return {[type]} [description]
*/
function remove(options) {
if (options.modName != 'calcDistance') {
close();
} else {
$("#calcDistanceModal").show();
}
}
function reset() {
$("#calcDistanceForm .startPointLon").val("");
$("#calcDistanceForm .startPointLat").val("");
$("#calcDistanceForm .stopPointLon").val("");
$("#calcDistanceForm .stopPointLat").val("");
$("#calcDistanceForm .outputResult").text("")
clearMarker();
removeMapClickEvent();
}
function close() {
$("#calcDistanceModal").hide();
reset();
}
// 执行
function execute() {
// 检测坐标
let startPointLon = $("#calcDistanceForm .startPointLon").val()
let startPointLat = $("#calcDistanceForm .startPointLat").val()
let stopPointLon = $("#calcDistanceForm .stopPointLon").val()
let stopPointLat = $("#calcDistanceForm .stopPointLat").val()
if (startPointLon == "" || startPointLat == "" || stopPointLon == "" || stopPointLat == "") {
alert("请完善转换参数")
throw "请完善转换参数 "
}
startPointLon = Number(startPointLon)
startPointLat = Number(startPointLat)
stopPointLon = Number(stopPointLon)
stopPointLat = Number(stopPointLat)
if (isNaN(startPointLon) || isNaN(startPointLat) || isNaN(stopPointLon) || isNaN(stopPointLat)) {
alert("请输入正确的数字格式")
throw "请输入正确的数字格式"
}
// 检测坐标范围
if (judge(startPointLon, startPointLat) && judge(stopPointLon, stopPointLat)) {
// 根据转换类型,访问接口获取转换后的数据
// 添加 转前点,转后点
getResult(startPointLon, startPointLat, stopPointLon, stopPointLat);
}
}
/**
* 获取输入信息并跳转到指定的坐标
* @type {Function}
* @private
*/
function judge(lng, lat) {
if (!L.Util.verifyLatLng(lat, lng)) {
alert('请正确输入经纬范围(经度0-180、纬度0-90)');
return false;
}
return true;
}
/**
* 获取并计算经纬度
* @private
*/
function getResult(startPointLon, startPointLat, stopPointLon, stopPointLat) {
$.ajax({
type: "get",
dataType: 'json',
url: onemapUrlConfig.PROXY_URL + "/calculateDistance",
data: {
lon1: startPointLon,
lat1: startPointLat,
lon2: stopPointLon,
lat2: stopPointLat,
type: 0
},
success: function (data) {
if (data.code == 200) {
$("#calcDistanceForm .outputResult").text(data.content.toFixed(6) + "米")
}
},
error: function (error) {
console.log(error)
}
});
return;
};
function addMarker(lon, lat, style, text) {
let entity = map3DViewer.map.entities.add({
name: '添加点',
position: Cesium.Cartesian3.fromDegrees(lon, lat, 0),
point: {
color: Cesium.Color.fromCssColorString(style.color), //颜色
pixelSize: 10, //大小
outlineColor: Cesium.Color.YELLOW, //轮廓颜色
outlineWidth: 1
},
label: {
pixelOffset: new Cesium.Cartesian2(0, style.labelOffset),
text: text,
font: '24px',
fillColor: Cesium.Color.fromCssColorString(style.color)
}
})
return entity._id;
}
/**
* 清除marker
* @return {[type]} [description]
*/
function clearMarker() {
if (modValue.startPointId) {
map3DViewer.map.entities.removeById(modValue.startPointId)
modValue.startPointId = ""
}
if (modValue.stopPointId) {
map3DViewer.map.entities.removeById(modValue.stopPointId)
modValue.stopPointId = ""
}
}
function clearSingleMarker(id, type) {
if (id) {
map3DViewer.map.entities.removeById(id)
modValue[type] = ""
return;
}
}
return ONEMAP.M.calcDistanceForm = {
open: open,
clearMarker: clearMarker
};
})