mapboxView.nvue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. //console.log(this.navdata)
  28. if(this.isAndroid){
  29. this.mapHeight=systemInfo.windowHeight*(750/systemInfo.windowWidth);
  30. }
  31. else{
  32. this.mapHeight=systemInfo.windowHeight*(750/systemInfo.windowWidth)-systemInfo.statusBarHeight*(750/systemInfo.windowWidth);
  33. }
  34. //检查并授权定位权限
  35. MapBox.checkLocationPermission(function(res){
  36. console.log(res);
  37. if (res.result == true) {
  38. //定位已授权
  39. } else {
  40. //定位权限杯拒绝
  41. }
  42. });
  43. //ios-------------------------------------------------------------
  44. if(!this.isAndroid){
  45. var that = this;
  46. uni.onPushMessage((res) => {
  47. //console.log("收到推送消息map:",res) //监听推送消息
  48. var payload=res.data.payload;
  49. if(res.type=='receive'){
  50. plus.runtime.setBadgeNumber(0);
  51. var content=res.data.content;
  52. if(content.indexOf("Call")!=-1){
  53. //getApp().globalData.iosfl(3);
  54. return
  55. }
  56. if(content.indexOf("Message")!=-1){
  57. //getApp().globalData.iosfl(4);
  58. return
  59. }
  60. }
  61. })
  62. }
  63. },
  64. mounted() {
  65. //导航view
  66. naviView = this.$refs.RnaviView;
  67. if(!this.isAndroid){
  68. //getApp().globalData.iosfl(1);
  69. this.getLocation();
  70. }
  71. },
  72. onUnload() {
  73. if(!this.isAndroid){
  74. //getApp().globalData.iosfl(2);
  75. }
  76. },
  77. methods: {
  78. i18n(str){
  79. return getApp().globalData.$t(str);
  80. },
  81. onViewCreated() {
  82. //设置导航回调
  83. var that = this;
  84. naviView.setNaviCallback(function(res){
  85. //console.log(res);
  86. if (res.type == "onLocationChanged"&&that.isAndroid) {
  87. //当前位置
  88. that.latitude = res.data.latitude;
  89. that.longitude = res.data.longitude;
  90. if(that.firstopen){
  91. that.firstopen=false;
  92. setTimeout(function() {
  93. // 这里写要延时执行的代码
  94. that.guihualuji();
  95. }, 2000);
  96. }
  97. } else if (res.type == "onActiveNavigation") {
  98. } else if (res.type == "onInfoPanelHidden") {
  99. //点击结束按钮,回调,关闭页面
  100. console.log('点击结束按钮');
  101. uni.navigateBack({});
  102. }
  103. });
  104. },
  105. getLocation(){
  106. var that = this;
  107. uni.getLocation({
  108. type: "wgs84", //默认为 wgs84 返回 gps 坐标//谷歌地图使用 wgs84 坐标,其他地图使用 gcj02 坐标
  109. geocode: "true",
  110. isHighAccuracy: "true",
  111. accuracy: "best", // 精度值为20m
  112. success: function (res) {
  113. //console.log("app定位获取:", res);
  114. if(res.longitude>-180&&res.longitude<180){
  115. if(res.latitude>-90&&res.latitude<90){
  116. that.latitude = res.latitude;
  117. that.longitude = res.longitude;
  118. if(that.firstopen){
  119. that.firstopen=false;
  120. setTimeout(function() {
  121. // 这里写要延时执行的代码
  122. that.guihualuji();
  123. }, 1000);
  124. }
  125. }
  126. }
  127. },
  128. fail(err) {
  129. console.log('getLocation err')
  130. }
  131. });
  132. },
  133. guihualuji(){
  134. var that = this;
  135. if(this.latitude==0||this.longitude==0){
  136. uni.showToast({
  137. title:that.i18n('api.dingweishibai'),
  138. icon: "none",
  139. duration: 2500
  140. })
  141. // setTimeout(function() {
  142. // // 这里写要延时执行的代码
  143. // uni.navigateBack();
  144. // }, 2500);
  145. return;
  146. }
  147. //路径规划
  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>