SWT:gc.drawPolyline

//GC.drawPolyline(int[] pointArray);这条语句绘制了一系列互相连接的线段,作为参数的数组用于描述点的位置
 gc.drawPolyline(pointArray);//若要形成闭合的图形,第一个点和最后一个点坐标必须一致

<template> <view> <view class="map" id="mapDiv" style="width: 100%;height: 80vh;z-index: 0;position: relative;" :prop="dataOption" :change:prop="Trenderjs.initTMap" :change:renderTrigger="Trenderjs.handleMessage" :renderTrigger="triggerCounter"> </view> </view> </template> <script> export default { data() { return { msg: '我是service层原来的msg', triggerCounter: 0, isProcessing: false, message: null }; }, props: { dataOption: { type: String, default: '' }, }, methods: { renderjsData() { if (this.isProcessing) return; this.isProcessing = true; this.triggerCounter++; }, someMethodInVue(newVal) { this.$emit('childEvent', newVal); this.message = newVal; this.isProcessing = false; } } }; </script> <script module="Trenderjs" lang="renderjs"> var control; export default { data() { return { tk: '27657afc3318f8f2166fceba83427734', options: { lng: '104.397894', lat: '31.126855', }, iconType: "default", dataList: [], pointList: [], Tmap: null, gisDataObj: {}, gisData: {}, geocode: null, gisPointLngLat: {}, // 新增定位优化相关变量 positionBuffer: [], // 位置缓冲区 bufferSize: 5, // 缓冲区大小 lastPosition: null, // 上次位置 lastTimestamp: 0, // 上次时间戳 realTimeMarker: null, // 实时定位标记 realTimeLabel: null // 实时定位标签 } }, mounted() { if (!window.T) { const script = document.createElement('script') script.src = 'http://api.tianditu.gov.cn/api?v=4.0&tk=' + this.tk document.head.appendChild(script) script.onload = this.initChartsRender.bind() } else { this.initMap(); } }, methods: { // 初始化地图 initMap() { const { lng, lat } = this.options; this.Tmap = new T.Map('mapDiv'); this.Tmap.centerAndZoom(new T.LngLat(lng, lat), 15); this.Tmap.setStyle("indigo"); this.Tmap.disableDoubleClickZoom(); this.Tmap.disableScrollWheelZoom(); this.Tmap.disableInertia(); this.Tmap.disableKeyboard(); this.geocode = new T.Geocoder(); control = new T.Control.Zoom(); this.Tmap.addControl(control); control.setPosition(T_ANCHOR_BOTTOM_RIGHT); }, // 若没有加载地图方法 initChartsRender() { this.initMap(); }, // 初始化地图数据 initTMap(newValue, oldValue) { let that = this; // 清除旧的定位监听 that._watchId && plus.geolocation.clearWatch(that._watchId); // 启动新的定位监听 that._watchId = plus.geolocation.watchPosition( position => that._handlePosition(position), err => console.log(err), { provider: 'system', enableHighAccuracy: true, maximumAge: 0, timeout: 10000 } ); // 处理传入的地图数据 if (newValue) { setTimeout(function() { try { that.gisDataObj = JSON.parse(newValue); that.gisData = JSON.parse(that.gisDataObj.data); let gisPoint = JSON.parse(that.gisData.data); that.gisPointLngLat = gisPoint; // 设置中心点 let centerLng, centerLat; if (that.gisData.type == 'marker') { centerLng = gisPoint.lng; centerLat = gisPoint.lat; } else if (that.gisData.type == 'polyline' || that.gisData.type == 'polygon') { centerLng = gisPoint[0].lng; centerLat = gisPoint[0].lat; } else { centerLng = '104.397894'; centerLat = '31.126855'; } that.Tmap.centerAndZoom(new T.LngLat(centerLng, centerLat), 15); // 绘制地图元素 that._drawMapElements(); } catch (e) { console.error('数据解析错误:', e); } }, 1000); } }, // 处理定位点 - 核心优化 _handlePosition(position) { const coords = position.coords; const now = Date.now(); const newPoint = { lng: coords.longitude, lat: coords.latitude, accuracy: coords.accuracy, timestamp: now }; console.log(`实时定位:${newPoint.lng.toFixed(6)},${newPoint.lat.toFixed(6)}`); // 应用移动平均滤波 const smoothedPoint = this._smoothPosition(newPoint); // 更新地图标记 this._updateRealTimeMarker(smoothedPoint); // 更新位置记录 this.lastPosition = smoothedPoint; this.lastTimestamp = now; }, // 移动平均滤波算法 _smoothPosition(newPoint) { // 添加到缓冲区 this.positionBuffer.push(newPoint); // 保持缓冲区大小 if (this.positionBuffer.length > this.bufferSize) { this.positionBuffer.shift(); } // 计算平均值 const sum = this.positionBuffer.reduce((acc, point) => { acc.lng += point.lng; acc.lat += point.lat; return acc; }, { lng: 0, lat: 0 }); return { lng: sum.lng / this.positionBuffer.length, lat: sum.lat / this.positionBuffer.length, accuracy: newPoint.accuracy }; }, // 更新实时定位标记 _updateRealTimeMarker(point) { const mapPoint = new T.LngLat(point.lng, point.lat); // 清除旧标记 if (this.realTimeMarker) this.Tmap.removeOverLay(this.realTimeMarker); if (this.realTimeLabel) this.Tmap.removeOverLay(this.realTimeLabel); // 创建新标记 this.realTimeMarker = new T.Marker(mapPoint); this.Tmap.addOverLay(this.realTimeMarker); // 创建文本标签 this.realTimeLabel = new T.Label({ text: `实时定位:${point.lng.toFixed(6)},${point.lat.toFixed(6)}`, position: mapPoint, offset: new T.Point(-40, 10) }); this.Tmap.addOverLay(this.realTimeLabel); // 平滑移动地图中心 this.Tmap.panTo(mapPoint); }, // 绘制地图元素 _drawMapElements() { if (!this.gisData || !this.gisPointLngLat) return; switch (this.gisData.type) { case 'marker': this._drawMarker(); break; case 'polyline': this._drawPolyline(); break; case 'polygon': this._drawPolygon(); break; } }, // 绘制标记点 _drawMarker() { const point = new T.LngLat(this.gisPointLngLat.lng, this.gisPointLngLat.lat); const marker = new T.Marker(point); this.Tmap.addOverLay(marker); const label = new T.Label({ text: this.gisData.name, position: point, offset: new T.Point(-40, 10) }); this.Tmap.addOverLay(label); }, // 绘制折线 _drawPolyline() { const points = this.gisPointLngLat.map(p => new T.LngLat(p.lng, p.lat) ); const polyline = new T.Polyline(points, { color: this.gisDataObj.color || '#FF0000', weight: 6 }); this.Tmap.addOverLay(polyline); // 添加终点标签 const endPoint = points[points.length - 1]; const label = new T.Label({ text: this.gisData.name, position: endPoint, offset: new T.Point(-40, 10) }); this.Tmap.addOverLay(label); }, // 绘制多边形 _drawPolygon() { const points = this.gisPointLngLat.map(p => new T.LngLat(p.lng, p.lat) ); const polygon = new T.Polygon(points, { color: this.gisDataObj.color || '#1E90FF', weight: 6 }); this.Tmap.addOverLay(polygon); // 添加中心点标签 const center = this._calculateCenter(points); const label = new T.Label({ text: this.gisData.name, position: center, offset: new T.Point(-40, 10) }); this.Tmap.addOverLay(label); }, // 计算多边形中心点 _calculateCenter(points) { const sum = points.reduce((acc, point) => { acc.lng += point.getLng(); acc.lat += point.getLat(); return acc; }, { lng: 0, lat: 0 }); return new T.LngLat( sum.lng / points.length, sum.lat / points.length ); }, // RenderJS事件处理器 handleMessage(newVal, oldVal) { if (newVal !== oldVal && this.gisPointLngLat) { const geolocation = new T.Geolocation(); geolocation.getCurrentPosition(e => { const distance = this.getDistance( e.lnglat.lat, e.lnglat.lng, this.gisPointLngLat.lat, this.gisPointLngLat.lng ); this.$ownerInstance.callMethod('someMethodInVue', { lng: e.lnglat.lng, lat: e.lnglat.lat, distance: distance * 1000 }); }); } }, // 计算两点间距离(Haversine公式) getDistance(lat1, lng1, lat2, lng2) { const radLat1 = (lat1 * Math.PI) / 180.0; const radLat2 = (lat2 * Math.PI) / 180.0; const a = radLat1 - radLat2; const b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0; const 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) ) ); return s * 6378.137; // 返回公里数 } } }; </script> 还是没有和搞个百度地图那样平稳的点运动轨迹
07-02
<template> <view> <view class="map" id="mapDiv" style="width: 100%;height: 80vh;z-index: 0;position: relative;" :prop="dataOption" :change:prop="Trenderjs.initTMap" :change:renderTrigger="Trenderjs.handleMessage" :renderTrigger="triggerCounter"> </view> </view> </template> <script> export default { data() { return { msg: '我是service层原来的msg', triggerCounter: 0, isProcessing: false, // 新增状态锁 message: null }; }, props: { dataOption: { type: String, default: '' }, }, methods: { renderjsData() { console.log('----------', this.triggerCounter); if (this.isProcessing) return; // 阻止重复触发 this.isProcessing = true; this.triggerCounter++ console.log('=============', this.triggerCounter); }, someMethodInVue(newVal) { console.log('位置', newVal); this.$emit('childEvent', newVal); this.message = newVal this.isProcessing = false; } } }; </script> <script module="Trenderjs" lang="renderjs"> var control; export default { data() { return { tk: '27657afc3318f8f2166fceba83427734', options: { lng: '104.397894', lat: '31.126855', }, iconType: "default", dataList: [], pointList: [], Tmap: null, gisDataObj: {}, gisData: {}, geocode: null, gisPointLngLat: {} } }, mounted() { if (!window.T) { console.log('00000', ); const script = document.createElement('script') script.src = 'http://api.tianditu.gov.cn/api?v=4.0&tk=' + this.tk // this.initChartsRender.bind() document.head.appendChild(script) script.onload = this.initChartsRender.bind() } else { console.log('++++', ); const { lng, lat } = this.options this.Tmap = null; console.log(this.Tmap); this.Tmap = new T.Map('mapDiv'); this.Tmap.centerAndZoom(new T.LngLat(lng, lat), 15); this.Tmap.setStyle("indigo"); //设置地图主体颜色indigo this.Tmap.disableDoubleClickZoom(); this.Tmap.disableScrollWheelZoom(); this.Tmap.disableInertia() this.Tmap.disableKeyboard() //创建对象 this.geocode = new T.Geocoder(); control = new T.Control.Zoom(); this.Tmap.addControl(control); var controlPosition = T_ANCHOR_BOTTOM_RIGHT; control.setPosition(controlPosition); // Tmap.enableDrag(); // this.setIcon(lng, lat, true, '') } }, created() { }, methods: { // 初始化地图 initTMap(newValue, oldValue) { let that = this; // 开启实时定位监听 that._watchId && plus.geolocation.clearWatch(that._watchId); // 先清除旧的监听 that._watchId = plus.geolocation.watchPosition(successCB => { let data = { lng: successCB.coords.longitude, lat: successCB.coords.latitude } console.log('实时定位:', data.lng, data.lat); that.Tmap.centerAndZoom(new T.LngLat(data.lng, data.lat), 20); var point = new T.LngLat(data.lng, data.lat); const marker = new T.Marker(point, { // icon }); that.Tmap.clearOverLays(); // 可选:每次清除旧的覆盖物 that.Tmap.addOverLay(marker); var label = new T.Label({ text: '实时定位:' + data.lng + ',' + data.lat, position: point, offset: new T.Point(-40, 10) }); //创建地图文本对象 that.Tmap.addOverLay(label); }, err => { console.log(err); }, { provider: 'system', enableHighAccuracy: true, }); // let that = this if (newValue) { setTimeout(function() { console.log(JSON.parse(newValue)); that.gisDataObj = JSON.parse(newValue) console.log(JSON.parse(that.gisDataObj.data)); that.gisData = JSON.parse(that.gisDataObj.data) // if (that.gisData.length > 0) { // that.options = { // lng: that.pointList[0].deviceDeploymentLocation.split(',')[1], // lat: that.pointList[0].deviceDeploymentLocation.split(',')[0] // } // } else { let gisPoint = JSON.parse(that.gisData.data) that.gisPointLngLat = JSON.parse(that.gisData.data) if (that.gisData.type == 'marker') { that.options = { lng: gisPoint.lng, lat: gisPoint.lat, } } else if (that.gisData.type == 'polyline' || that.gisData.type == 'polygon') { that.options = { lng: gisPoint[0].lng, lat: gisPoint[0].lat, } } else { that.options = { lng: '104.397894', lat: '31.126855', } } const { lng, lat } = that.options that.Tmap.centerAndZoom(new T.LngLat(lng, lat), 15); if (that.gisData.type == 'marker') { var point = new T.LngLat(gisPoint.lng, gisPoint.lat); console.log(point); const marker = new T.Marker(point, { // icon }); that.Tmap.addOverLay(marker); var label = new T.Label({ text: that.gisData.name, position: point, offset: new T.Point(-40, 10) }); //创建地图文本对象 that.Tmap.addOverLay(label); } else if (that.gisData.type == 'polyline') { let points = []; for (let i = 0; i < gisPoint.length; i++) { points.push(new T.LngLat(gisPoint[i].lng, gisPoint[i].lat)); } //创建线对象 var line = new T.Polyline(points, { color: that.gisDataObj.color, weight: 6, }); //向地图上添加线 that.Tmap.addOverLay(line); var label = new T.Label({ text: that.gisData.name, position: points[points.length - 1], offset: new T.Point(-40, 10) }); //创建地图文本对象 that.Tmap.addOverLay(label); } else if (that.gisData.type == 'polygon') { let points = []; for (let i = 0; i < gisPoint.length; i++) { points.push(new T.LngLat(gisPoint[i].lng, gisPoint[i].lat)); } //创建线对象 var polygon = new T.Polygon(points, { color: that.gisDataObj.color, weight: 6, }); //向地图上添加线 that.Tmap.addOverLay(polygon); var label = new T.Label({ text: that.gisData.name, position: points[points.length - 1], offset: new T.Point(-40, 10) }); //创建地图文本对象 that.Tmap.addOverLay(label); } }, 1000); } }, // 若没有加载地图方法 initChartsRender() { const { lng, lat } = this.options var that = this; this.Tmap = new T.Map('mapDiv'); this.Tmap.centerAndZoom(new T.LngLat(lng, lat), 15); this.Tmap.setStyle("indigo"); //设置地图主体颜色indigo this.Tmap.disableDoubleClickZoom(); this.Tmap.disableScrollWheelZoom(); this.Tmap.disableInertia() this.Tmap.disableKeyboard() control = new T.Control.Zoom(); this.Tmap.addControl(control); var controlPosition = T_ANCHOR_BOTTOM_RIGHT; control.setPosition(controlPosition); // Tmap.enableDrag(); // this.setIcon(lng, lat, true, '') }, // 地图图标Icon setIcon(lng, lat, isClear, markerActive) { if (isClear) { // if (this.iconType == 'searchIcon') { // this.Tmap.clearOverLays() // } else { this.Tmap.removeOverLay(markerActive) // } } console.log(this.gisData); let gisPoint = JSON.parse(this.gisData.data) }, // RenderJS事件处理器 handleMessage(newVal, oldVal) { console.log(newVal, oldVal); if (newVal !== oldVal) { const center = this.Tmap.getCenter(); var lo = new T.Geolocation(); let that = this // let gisPoint = JSON.parse(this.gisData.data) const fn = function(e) { let distance = that.getDistance(e.lnglat.lat, e.lnglat.lng, that.gisPointLngLat.lat, that .gisPointLngLat.lng) that.$ownerInstance.callMethod('someMethodInVue', { lng: e.lnglat.lng, lat: e.lnglat.lat, distance: distance * 1000 }); // // if (this.getStatus() == 0) { // that.Tmap.centerAndZoom(e.lnglat, 15) // console.log('e.lnglat000', e.lnglat.lat, e.lnglat.lng); // // var marker = new T.Marker(e.lnglat); // // that.Tmap.addOverLay(marker); // // } // // if (this.getStatus() == 1) { // console.log('e.lnglat111', e.lnglat.lat, e.lnglat.lng); // that.Tmap.centerAndZoom(e.lnglat, e.level) // // var marker = new T.Marker(e.lnglat); // // that.Tmap.addOverLay(marker); // // } } lo.getCurrentPosition(fn); } }, getDistance(lat1, lng1, lat2, lng2) { console.log(lat1, lng1, lat2, lng2); const radLat1 = (lat1 * Math.PI) / 180.0 const radLat2 = (lat2 * Math.PI) / 180.0 const a = radLat1 - radLat2 const b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0 let 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 *= 6378.137 // EARTH_RADIUS; s = Math.round(s * 100) / 100 console.log(s); return s // 调用 return的距离单位为km } }, } </script> 只在这段代码的基础上修复漂移的问题即可
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值