| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- 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
- },
- 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 += "<tr><td><span>" + (i + 1) + ".</span></td><td><span>" + data[i].instruction + "</span></td></tr>"
- // 绘制路径 存储
- let polylineItem = data[i].polyline.split(";")
- // .map(function (item) {
- // let coor = item.split(",")
- // return Cesium.CoordTransform.coordinate.gcj02_to_wgs84(Number(coor[0]), Number(coor[1]))
- // })
- .join(",").split(",").map(function (num) {
- return Number(num);
- })
- polyline.push(polylineItem)
- }
- modValue.polylineEntity = map3DViewer.map.entities.add({
- polyline: {
- positions: Cesium.Cartesian3.fromDegreesArray(polyline.join(",").split(",").map(function (num) {
- return Number(num);
- })),
- width: 5,
- material: Cesium.Color.RED,
- clampToGround: true,
- },
- })
- $("#pathPlanningForm .outputResult").append(html);
- }
- 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
- */
- 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 clearPolyline() {
- if (modValue.polylineEntity) {
- map3DViewer.map.entities.remove(modValue.polylineEntity)
- modValue.polylineEntity = null;
- }
- }
- function clearSingleMarker(id, type) {
- if (id) {
- map3DViewer.map.entities.removeById(id)
- modValue[type] = ""
- return;
- }
- }
- return ONEMAP.M.pathPlanningForm = {
- open: open,
- clearMarker: clearMarker,
- clearPolyline: clearPolyline
- };
- })
|