define(['html!templates/menu/baseCalc/pathPlanning', 'css!styles/tools/toolPublicPopup', 'css!styles/menu/baseCalc/pathPlanning', ], 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, }, polylineEntity: null }; /** * 初始化并订阅事件 * @return {[type]} [description] */ function open(data) { if (!status.initialized) { setLayout(); bindEvent(); subscribe(); status.initialized = true; } else { // 重置 reset(); } modValue.data = data; $("#pathPlanningModal .title").text(data.label); ONEMAP.C.publisher.publish({ modName: 'pathPlanning' }, 'baseCalc:active'); }; function setLayout() { $('body').append(tplLayout); //拖拽 $("#pathPlanningModal .popup-ct").dragmove($('#pathPlanningModal')); }; /** * 事件绑定 */ function bindEvent() { $("#pathPlanningModal .close").bind('click', function () { close(); }); $("#pathPlanningModal .btn-default.sure").bind('click', function () { execute() }); $("#pathPlanningModal .btn-default.clickStart").bind('click', function () { clearPolyline(); getMapClickEvent(function (lonlat) { clearSingleMarker(modValue.startPointId, "startPointId") $("#pathPlanningForm .startPointLon").val(lonlat.lon); $("#pathPlanningForm .startPointLat").val(lonlat.lat); modValue.startPointId = addMarker(lonlat.lon, lonlat.lat, modValue.startPointStyle, "起点") }) }); $("#pathPlanningModal .btn-default.clickStop").bind('click', function () { clearPolyline(); getMapClickEvent(function (lonlat) { clearSingleMarker(modValue.stopPointId, "stopPointId") $("#pathPlanningForm .stopPointLon").val(lonlat.lon); $("#pathPlanningForm .stopPointLat").val(lonlat.lat); modValue.stopPointId = addMarker(lonlat.lon, lonlat.lat, modValue.stopPointStyle, "终点") }) }); $("#pathPlanningModal .btn-default.cleanStart").bind('click', function () { $("#pathPlanningForm .startPointLon").val(""); $("#pathPlanningForm .startPointLat").val(""); clearSingleMarker(modValue.startPointId, "startPointId") }); $("#pathPlanningModal .btn-default.clearStop").bind('click', function () { $("#pathPlanningForm .stopPointLon").val(""); $("#pathPlanningForm .stopPointLat").val(""); clearSingleMarker(modValue.stopPointId, "stopPointId") }); //回车执行 $('#pathPlanningInput, #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 != 'pathPlanning') { close(); } else { $("#pathPlanningModal").show(); } } function reset() { $("#pathPlanningForm .startPointLon").val(""); $("#pathPlanningForm .startPointLat").val(""); $("#pathPlanningForm .stopPointLon").val(""); $("#pathPlanningForm .stopPointLat").val(""); $("#pathPlanningForm .outputResult").text("") clearMarker(); clearPolyline(); removeMapClickEvent(); } function close() { $("#pathPlanningModal").hide(); reset(); } // 执行 function execute() { // 检测坐标 let startPointLon = $("#pathPlanningForm .startPointLon").val() let startPointLat = $("#pathPlanningForm .startPointLat").val() let stopPointLon = $("#pathPlanningForm .stopPointLon").val() let stopPointLat = $("#pathPlanningForm .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) { // let startArr = Cesium.CoordTransform.coordinate.wgs84_to_gcj02(startPointLon, startPointLat) // let stopArr = Cesium.CoordTransform.coordinate.wgs84_to_gcj02(stopPointLon, stopPointLat) $.ajax({ type: "get", dataType: 'json', url: onemapUrlConfig.PROXY_URL + "/driving", data: { origin: startPointLon + "," + startPointLat, destination: stopPointLon + "," + stopPointLat, // origin: Math.floor(startArr[0] * 1000000) / 1000000 + "," + Math.floor(startArr[1] * 1000000) / 1000000, // destination: Math.floor(stopArr[0] * 1000000) / 1000000 + "," + Math.floor(stopArr[1] * 1000000) / 1000000, type: 0, proxyToken: localStorage.getItem("systemToken"), }, success: function (data) { if (data.status == 1) { setOutputResult(data.route.paths[0].steps) // $("#pathPlanningForm .outputResult").text(data.content.toFixed(6) + "米") } }, error: function (error) { console.log(error) } }); return; }; function setOutputResult(data) { $("#pathPlanningForm .outputResult").empty() let html = ""; let polyline = [] for (let i = 0; i < data.length; i++) { html += "