mapboxView.nvue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <view>
  3. <mapbox_navi_view ref="RnaviView" :style="'width: 100%;height:'+mapHeight+'rpx;'"
  4. @onViewCreated="onViewCreated"></mapbox_navi_view>
  5. </view>
  6. </template>
  7. <script>
  8. var MapBox = uni.requireNativePlugin('MapBox-Plugin');
  9. var naviView=null
  10. export default {
  11. data() {
  12. return {
  13. latitude:0,
  14. longitude:0,
  15. navdata:'',
  16. firstopen:true,
  17. isAndroid:false,
  18. mapHeight:0,
  19. }
  20. },
  21. onLoad(option) {
  22. let systemInfo = uni.getSystemInfoSync();
  23. this.isAndroid = systemInfo.platform.toLowerCase() === 'android';
  24. if(option.navdata){
  25. this.navdata=JSON.parse(option.navdata);
  26. }
  27. if(this.isAndroid){
  28. this.mapHeight=systemInfo.windowHeight*(750/systemInfo.windowWidth);
  29. }
  30. else{
  31. this.mapHeight=systemInfo.windowHeight*(750/systemInfo.windowWidth)-systemInfo.statusBarHeight*(750/systemInfo.windowWidth);
  32. }
  33. //检查并授权定位权限
  34. MapBox.checkLocationPermission(function(res){
  35. console.log(res);
  36. if (res.result == true) {
  37. //定位已授权
  38. } else {
  39. //定位权限杯拒绝
  40. }
  41. });
  42. //ios-------------------------------------------------------------
  43. if(!this.isAndroid){
  44. var that = this;
  45. uni.onPushMessage((res) => {
  46. console.log("收到推送消息map:",res) //监听推送消息
  47. var payload=res.data.payload;
  48. if(res.type=='receive'){
  49. plus.runtime.setBadgeNumber(0);
  50. var content=res.data.content;
  51. if(content.indexOf("Call")!=-1){
  52. //getApp().globalData.iosfl(3);
  53. return
  54. }
  55. if(content.indexOf("Message")!=-1){
  56. //getApp().globalData.iosfl(4);
  57. return
  58. }
  59. }
  60. })
  61. }
  62. },
  63. mounted() {
  64. //导航view
  65. naviView = this.$refs.RnaviView;
  66. if(!this.isAndroid){
  67. //getApp().globalData.iosfl(1);
  68. this.getLocation();
  69. }
  70. },
  71. onUnload() {
  72. if(!this.isAndroid){
  73. //getApp().globalData.iosfl(2);
  74. }
  75. },
  76. methods: {
  77. i18n(str){
  78. return getApp().globalData.$t(str);
  79. },
  80. onViewCreated() {
  81. //设置导航回调
  82. var that = this;
  83. naviView.setNaviCallback(function(res){
  84. //console.log(res);
  85. if (res.type == "onLocationChanged"&&that.isAndroid) {
  86. //当前位置
  87. that.latitude = res.data.latitude;
  88. that.longitude = res.data.longitude;
  89. if(that.firstopen){
  90. that.firstopen=false;
  91. setTimeout(function() {
  92. // 这里写要延时执行的代码
  93. that.guihualuji();
  94. }, 2000);
  95. }
  96. } else if (res.type == "onActiveNavigation") {
  97. } else if (res.type == "onInfoPanelHidden") {
  98. //点击结束按钮,回调,关闭页面
  99. console.log('点击结束按钮');
  100. uni.navigateBack({});
  101. }
  102. });
  103. },
  104. getLocation(){
  105. var that = this;
  106. uni.getLocation({
  107. type: "wgs84", //默认为 wgs84 返回 gps 坐标//谷歌地图使用 wgs84 坐标,其他地图使用 gcj02 坐标
  108. geocode: "true",
  109. isHighAccuracy: "true",
  110. accuracy: "best", // 精度值为20m
  111. success: function (res) {
  112. //console.log("app定位获取:", res);
  113. if(res.longitude>-180&&res.longitude<180){
  114. if(res.latitude>-90&&res.latitude<90){
  115. that.latitude = res.latitude;
  116. that.longitude = res.longitude;
  117. if(that.firstopen){
  118. that.firstopen=false;
  119. setTimeout(function() {
  120. // 这里写要延时执行的代码
  121. that.guihualuji();
  122. }, 1000);
  123. }
  124. }
  125. }
  126. },
  127. fail(err) {
  128. console.log('getLocation err')
  129. }
  130. });
  131. },
  132. guihualuji(){
  133. var that = this;
  134. if(this.latitude==0||this.longitude==0){
  135. uni.showToast({
  136. title:that.i18n('api.dingweishibai'),
  137. icon: "none",
  138. duration: 2500
  139. })
  140. // setTimeout(function() {
  141. // // 这里写要延时执行的代码
  142. // uni.navigateBack();
  143. // }, 2500);
  144. return;
  145. }
  146. //路径规划
  147. console.log('guihualuji')
  148. var navType=uni.getStorageSync('navType');
  149. if(''==navType||null==navType||undefined==navType){
  150. navType='cycling';
  151. uni.setStorageSync('navType','cycling');
  152. }
  153. naviView.requestRoutes({
  154. coordinatesList:[
  155. {
  156. latitude:this.latitude,
  157. longitude:this.longitude
  158. },
  159. {
  160. latitude:this.navdata.latitude,
  161. longitude:this.navdata.longitude
  162. }
  163. ],//[起点,途经点(可选),终点]
  164. steps:true,
  165. bannerInstructions:true,
  166. voiceInstructions:true,
  167. profile:navType//driving-traffic driving walking cycling
  168. }, function(res){
  169. console.log(res);
  170. if (res.type == "onRoutesReady") {//路径规划成功
  171. //预览路线
  172. naviView.startRoutePreview();
  173. if(!that.isAndroid){
  174. setTimeout(function() {
  175. // 这里写要延时执行的代码
  176. that.startTripSession();
  177. }, 3500);
  178. }
  179. }
  180. });
  181. },
  182. startTripSession() {
  183. console.log('startTripSession')
  184. //开始导航
  185. naviView.startTripSession();
  186. },
  187. }
  188. }
  189. </script>
  190. <style>
  191. </style>