index.nvue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <template>
  2. <div>
  3. <view class="map">
  4. <map id="map" ref="map" :polyline="polyline" :scale="scale" :style="{height: windowHeight}"
  5. :markers="markers" :polygons="polygons" :latitude="latitude" :longitude="longitude" @tap="tap"
  6. :show-location="true" @markertap="markertap" @poitap="poitap" enable-rotate enable-overlooking
  7. enable-poi>
  8. </map>
  9. </view>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. barHeight: null, //动态高度初始值
  17. windowHeight: null,
  18. needBack: true,
  19. latitude: 39.998204,
  20. longitude: 116.481053,
  21. value: '1223',
  22. markers: [{
  23. id: 1,
  24. width: 55,
  25. height: 78,
  26. latitude: 39.997761,
  27. longitude: 116.478928,
  28. iconPath: "/static/images/path/star.png",
  29. anchor: {
  30. x: 0.5,
  31. y: 0.5,
  32. }
  33. }, {
  34. id: 2,
  35. width: 41,
  36. height: 47,
  37. latitude: 39.999844,
  38. longitude: 116.484674,
  39. iconPath: "/static/images/path/end.png",
  40. anchor: {
  41. x: 0.5,
  42. y: 1,
  43. },
  44. }],
  45. polygons: [{
  46. points: [
  47. {
  48. longitude: "116.477462800",
  49. latitude: "39.994629400"
  50. },
  51. {
  52. longitude: "116.475796400",
  53. latitude: "39.999226200"
  54. },
  55. {
  56. longitude: "116.486139200",
  57. latitude: "40.002975600"
  58. },
  59. {
  60. longitude: "116.487805600",
  61. latitude: "39.998378800"
  62. },
  63. ],
  64. fillColor: '#376af64d',
  65. width: 5,
  66. strokeColor: '#9BC2FC', //描边颜色
  67. strokeWidth: 2, //描边宽度
  68. zIndex: 100, //层级
  69. }],
  70. polyline: [{
  71. points: [{
  72. "longitude": 116.478928,
  73. "latitude": 39.997761
  74. },
  75. {
  76. "longitude": 116.478907,
  77. "latitude": 39.998422
  78. },
  79. {
  80. "longitude": 116.479384,
  81. "latitude": 39.998546
  82. },
  83. {
  84. "longitude": 116.481053,
  85. "latitude": 39.998204
  86. },
  87. {
  88. "longitude": 116.481793,
  89. "latitude": 39.997868
  90. },
  91. {
  92. "longitude": 116.482898,
  93. "latitude": 39.998217
  94. },
  95. {
  96. "longitude": 116.483789,
  97. "latitude": 39.999063
  98. },
  99. {
  100. "longitude": 116.484674,
  101. "latitude": 39.999844
  102. }
  103. ],
  104. arrowLine: true,
  105. color: 'rgba(119, 171, 238, 1)',
  106. width: 20,
  107. borderColor: 'rgba(55, 106, 246, 1)',
  108. borderWidth: 20,
  109. dottedLine: true,
  110. }],
  111. scale: 16,
  112. title: '',
  113. msg: '',
  114. msg2: '',
  115. intTime: 0,
  116. maxTime: 0,
  117. minTime: 0,
  118. totalTime: 0,
  119. initTime: new Date().getTime(),
  120. lastTime: 0,
  121. nowtime: 0,
  122. locTime: 0,
  123. reTime: 0,
  124. count: 0,
  125. errorCount: 0,
  126. }
  127. },
  128. computed: {},
  129. onLoad() {
  130. this.fitNav();
  131. },
  132. methods: {
  133. oneDodt(){
  134. fUN_AmapLocation.once({}, result => {
  135. console.log('====fUN_AmapLocation::8秒后单次定位示例====', JSON.stringify(result));
  136. // _this.msg = JSON.stringify(result);
  137. });
  138. },
  139. tap(e){
  140. console.log(e);
  141. },
  142. getData(latitude,longitude){
  143. let _this=this;
  144. // // 为做测试,我是对 polyline 点集合中最后一个点进行相对加一个数,让它有效果出来
  145. // let latitude = _this.polyline[0].points[_this.polyline[0].points.length - 1].latitude + 0.0001;
  146. // let longitude = _this.polyline[0].points[_this.polyline[0].points.length - 1].longitude + 0.0001;
  147. // 将新的点结构起来
  148. let dat = {latitude, longitude};
  149. // 把原 polyline 的数据复制一份
  150. let polyline_obj = JSON.parse(JSON.stringify(_this.polyline[0]));
  151. // 然后将新的点放到复制之后的这个对象里面去
  152. polyline_obj.points.push(dat);
  153. // 重组对象,让它和原 polyline 的数据结构一样
  154. let polyline = [polyline_obj, ];
  155. // 然后整体再赋值给原 polyline
  156. _this.polyline = polyline;
  157. // 最后修改中心经纬度
  158. _this.latitude = latitude;
  159. _this.longitude = longitude;
  160. },
  161. //绘制轨迹
  162. path(){
  163. let polyline_obj = JSON.parse(JSON.stringify(this.polyline[0]));
  164. polyline_obj.points = [
  165. {
  166. "longitude": 116.478928,
  167. "latitude": 39.997761
  168. },
  169. {
  170. "longitude": 116.478907,
  171. "latitude": 39.998422
  172. },
  173. {
  174. "longitude": 116.479384,
  175. "latitude": 39.998546
  176. },
  177. {
  178. "longitude": 116.481053,
  179. "latitude": 39.998204
  180. },
  181. {
  182. "longitude": 116.481793,
  183. "latitude": 39.997868
  184. },
  185. {
  186. "longitude": 116.482898,
  187. "latitude": 39.998217
  188. },
  189. {
  190. "longitude": 116.483789,
  191. "latitude": 39.999063
  192. },
  193. {
  194. "longitude": 116.484674,
  195. "latitude": 39.999844
  196. }
  197. ]
  198. let polyline = [polyline_obj, ];
  199. // 然后整体再赋值给原 polyline
  200. this.polyline = polyline;
  201. this.longitude = "116.478928"
  202. this.latitude = "39.997761"
  203. },
  204. //绘制矩形
  205. graph(){
  206. let polygons_obj = JSON.parse(JSON.stringify(this.polygons[0]));
  207. polygons_obj.points =[
  208. { // 右上
  209. latitude: 30.264786,
  210. longitude: 120.10775,
  211. }, { // 右下
  212. latitude: 30.268786,
  213. longitude: 120.10775,
  214. },
  215. { // 左下
  216. latitude: 30.268786,
  217. longitude: 120.10465,
  218. }, { // 左上
  219. latitude: 30.264786,
  220. longitude: 120.10575,
  221. }
  222. ]
  223. let polygons = [polygons_obj, ];
  224. // 然后整体再赋值给原 polygons
  225. this.polygons = polygons;
  226. console.log('矩形重画完毕')
  227. this.longitude = "120.10775"
  228. this.latitude = "30.264786"
  229. },
  230. //点击选择地址
  231. addressChoose() {
  232. uni.chooseLocation({
  233. success: (res) => {
  234. // latitude:this.latitude,
  235. // longitude:this.longitude,
  236. // console.log('位置名称:' + res.name);
  237. // console.log('详细地址:' + res.address);
  238. // console.log('纬度:' + res.latitude);
  239. // console.log('经度:' + res.longitude);
  240. this.longitude = res.longitude
  241. this.latitude = res.latitude
  242. this.markeMove()
  243. }
  244. });
  245. },
  246. markeMove() {
  247. this.$nextTick(() => {
  248. this.scale = 16
  249. // 更新Markers点
  250. uni.createMapContext("map", this).translateMarker({
  251. markerId: 'myId',
  252. destination: {
  253. latitude: this.latitude,
  254. longitude: this.longitude
  255. },
  256. });
  257. })
  258. },
  259. // 获取手机信息及位置
  260. fitNav() {
  261. // 获取手机信息
  262. let info = uni.getSystemInfoSync()
  263. //顶部高度
  264. this.barHeight = info.statusBarHeight
  265. this.windowHeight = info.windowHeight
  266. console.log('尺寸', info)
  267. },
  268. //返回
  269. back() {
  270. uni.navigateBack({
  271. delta: 1
  272. })
  273. },
  274. //保存
  275. saveBtn() {
  276. console.log('保存')
  277. },
  278. //获取当前位置
  279. getLocation() {
  280. uni.getLocation({
  281. type: 'gcj02',
  282. success: (res) => {
  283. console.log('当前位置的经度:' + res.longitude);
  284. console.log('当前位置的纬度:' + res.latitude);
  285. this.longitude = res.longitude
  286. this.latitude = res.latitude
  287. // this.move()
  288. this.markeMove()
  289. }
  290. });
  291. },
  292. //移动到此位置
  293. move() {
  294. this.$nextTick(() => {
  295. this.scale = 16
  296. uni.createMapContext("map", this).moveToLocation({
  297. longitude: this.longitude,
  298. latitude: this.latitude,
  299. });
  300. })
  301. },
  302. //放大
  303. add() {
  304. uni.createMapContext("map", this).getScale({
  305. success: (res) => {
  306. // console.log('返回值', res);
  307. this.scale = res.scale
  308. if (this.scale < 20) {
  309. this.scale += 1
  310. }
  311. }
  312. })
  313. },
  314. //缩小
  315. sub() {
  316. uni.createMapContext("map", this).getScale({
  317. success: (res) => {
  318. // console.log('返回值', res);
  319. this.scale = res.scale
  320. if (this.scale > 3) {
  321. this.scale -= 1
  322. }
  323. }
  324. })
  325. },
  326. //标记点的点击事件
  327. markertap(e) {
  328. // console.log('点击markertap', e)
  329. uni.openLocation({
  330. latitude: this.latitude,
  331. longitude: this.longitude,
  332. success: function() {
  333. // console.log('success');
  334. }
  335. });
  336. },
  337. //地图的点击事件
  338. poitap(e) {
  339. // console.log('点击maps', e.detail)
  340. this.longitude = e.detail.longitude
  341. this.latitude = e.detail.latitude
  342. this.markeMove()
  343. },
  344. //打开应用设置页
  345. settingUp() {
  346. fUN_AmapLocation.openSetting(function(result) {
  347. });
  348. },
  349. // 获取权限
  350. permission() {
  351. _this = this;
  352. console.log('获取权限');
  353. fUN_AmapLocation.permission({}, result => {
  354. console.log('====fUN_AmapLocation定位====', JSON.stringify(result));
  355. if (result.code === 0) {
  356. if (result.flag === true && result.flag_br === true) {
  357. _this.battery()
  358. } else {
  359. uni.showModal({
  360. title: '提醒',
  361. showCancel: true,
  362. content: '请在权限-位置信息中选择始终允许',
  363. success: function(res) {
  364. if (res.confirm) {
  365. _this.settingUp();
  366. } else if (res.cancel) {
  367. // alert('用户点击取消');
  368. }
  369. }
  370. })
  371. }
  372. }
  373. });
  374. },
  375. //开启电池优化
  376. battery() {
  377. let _that = this;
  378. //检测是否忽略电池优化(简单解释:开启电池优化,会干掉应用)
  379. fUN_AmapLocation.isIgnoringBatteryOptimizations(function(result) { //SDK>=23,否则返回不支持
  380. if (result.code === 0) {
  381. if (result.flag === true) {
  382. console.log('====忽略电池优化====')
  383. _that.count = 0;
  384. _that.start();
  385. _that.openWakeLock();
  386. } else {
  387. // 申请忽略电池优化(如果已加入电池优化的白名单 则进入系统电池优化页面,若未加入则弹窗申请。)
  388. fUN_AmapLocation.requestIgnoreBatteryOptimizations(function(result) {
  389. console.log(result)
  390. console.log('====申请电池优化====')
  391. });
  392. }
  393. }
  394. })
  395. },
  396. //打开屏幕唤醒
  397. openWakeLock() {
  398. //1台华为[鸿蒙],2台小米(米6[安卓9]、米11Ultra[安卓11])使用的10分测试的。
  399. fUN_AmapLocation.openWakeLock({
  400. time: 5
  401. }, function(result) {
  402. console.log('====被唤醒了====', JSON.stringify(result))
  403. // if(result.code===0)
  404. // {
  405. // }
  406. });
  407. //参数 time:间隔时间,单位:分,建议5-10。具体看业务
  408. },
  409. //关闭屏幕唤醒
  410. colseWakeLock() {
  411. fUN_AmapLocation.colseWakeLock(function(result) {
  412. console.log('====结束被唤醒了====', JSON.stringify(result))
  413. });
  414. },
  415. // 结束持续定位
  416. end() {
  417. let _taht = this;
  418. fUN_AmapLocation.stop({}, result => {
  419. _taht.colseWakeLock();
  420. // console.log('====fUN_AmapLocation定位stop====', JSON.stringify(result));
  421. });
  422. },
  423. //开启持续定位
  424. start() {
  425. console.log('==fUN_AmapLocation==:开启定位');
  426. fUN_AmapLocation.start({
  427. intervalTime: 3000,
  428. isReport: false, //false 以下可以不传
  429. reportInterval: 6, //每定位几次进行上传
  430. url: 'http://192.168.0.66/fun/open/test_json.do', //上传URL
  431. params: {
  432. a: 1,
  433. B: '测试',
  434. c: true
  435. }, //上传的自定义参数,如加入其它数据
  436. headers: {
  437. a: '123',
  438. B: 'abcd'
  439. } //request headers 参数 比如加上token
  440. },
  441. result => {
  442. console.log(result);
  443. console.log('====fUN_AmapLocation定位====', JSON.stringify(result));
  444. if (result.code === 0) {
  445. _this.count++;
  446. _this.getData(result.latitude,result.longitude)
  447. } else {
  448. _this.errorCount++;
  449. }
  450. var now = new Date();
  451. _this.nowtime = now.getTime();
  452. _this.intTime = _this.nowtime - _this.lastTime;
  453. if (_this.intTime > _this.maxTime && _this.lastTime != 0) {
  454. _this.maxTime = _this.intTime;
  455. }
  456. if (_this.intTime < _this.minTime) {
  457. _this.minTime = _this.intTime;
  458. }
  459. _this.totalTime = _this.lastTime - _this.initTime;
  460. _this.lastTime = _this.nowtime;
  461. _this.msg2 = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
  462. _this.msg = JSON.stringify(result);
  463. _this.reTime = result.now;
  464. _this.locTime = result.time;
  465. }
  466. );
  467. }
  468. }
  469. }
  470. </script>
  471. <style scoped>
  472. .my-top-nvue {
  473. width: 750rpx;
  474. height: 88rpx;
  475. background-color: #ffffff;
  476. display: flex;
  477. flex-direction: row;
  478. justify-content: space-between;
  479. align-items: center;
  480. padding: 0 30rpx;
  481. /* border-width: 1px;
  482. border-style: solid;
  483. border-color: red; */
  484. }
  485. .my-top-nvue-back {
  486. width: 20rpx;
  487. height: 35rpx;
  488. }
  489. .my-top-nvue-words {
  490. font-size: 36rpx;
  491. color: #323232;
  492. }
  493. .my-top-nvue-save {
  494. font-size: 30rpx;
  495. color: #42c2a4;
  496. }
  497. .cover-view-input {
  498. width: 650rpx;
  499. height: 90rpx;
  500. background-color: #ffffff;
  501. border-radius: 10rpx;
  502. position: absolute;
  503. top: 50rpx;
  504. left: 50rpx;
  505. display: flex;
  506. flex-direction: row;
  507. align-items: center;
  508. padding: 0 20rpx;
  509. }
  510. .searchImg {
  511. width: 26rpx;
  512. height: 26rpx;
  513. /* border-width: 1px;
  514. border-style: solid;
  515. border-color: red; */
  516. }
  517. .searchWords {
  518. margin-left: 15rpx;
  519. /* border-width: 1px;
  520. border-style: solid;
  521. border-color: red; */
  522. }
  523. .searchWords-text {
  524. font-size: 28rpx;
  525. color: #969696;
  526. }
  527. .cover-view-loca {
  528. width: 90rpx;
  529. height: 90rpx;
  530. background-color: #ffffff;
  531. border-radius: 20rpx;
  532. position: absolute;
  533. bottom: 60rpx;
  534. left: 60rpx;
  535. display: flex;
  536. align-items: center;
  537. justify-content: center;
  538. }
  539. .cover-loca-image {
  540. width: 40rpx;
  541. height: 40rpx;
  542. }
  543. .cover-view-plus {
  544. width: 90rpx;
  545. height: 180rpx;
  546. background-color: #ffffff;
  547. border-radius: 20rpx;
  548. position: absolute;
  549. bottom: 60rpx;
  550. right: 60rpx;
  551. display: flex;
  552. align-items: center;
  553. justify-content: center;
  554. }
  555. .plus-btn {
  556. width: 90rpx;
  557. height: 90rpx;
  558. display: flex;
  559. align-items: center;
  560. justify-content: center;
  561. }
  562. .btn-line {
  563. width: 90rpx;
  564. height: 1rpx;
  565. border-width: 1px;
  566. border-style: solid;
  567. border-color: #ececec;
  568. }
  569. .btn-img {
  570. width: 36rpx;
  571. height: 36rpx;
  572. }
  573. </style>