当前位置:   article > 正文

uniapp使用高德地图和百度地图导航功能_地图导航uni-app

地图导航uni-app
  1. <template>
  2. <view class="map-container">
  3. <u-navbar :title="title" :autoBack="true"> </u-navbar>
  4. <view class="content" style="margin-top: 150rpx">
  5. <map
  6. id="map"
  7. class="map"
  8. :show-location="true"
  9. :latitude="mlat"
  10. :longitude="mlng"
  11. :markers="markers"
  12. ></map>
  13. </view>
  14. <u-button
  15. class="button"
  16. type="primary"
  17. text="到这里"
  18. @click="gomapApp"
  19. ></u-button>
  20. <view class="loading" v-if="loading">
  21. <u-loading-icon text="加载中" :vertical="true"></u-loading-icon>
  22. </view>
  23. <u-toast ref="uToast"></u-toast>
  24. </view>
  25. </template>
  26. <script>
  27. // const img = '/static/marken.png';
  28. export default {
  29. data() {
  30. return {
  31. lng: "",
  32. lat: "",
  33. mlng: "",
  34. mlat: "",
  35. markers: [],
  36. loading: false,
  37. _mapContext: "",
  38. title: "",
  39. deptId: "",
  40. };
  41. },
  42. onReady() {},
  43. onLoad(params) {
  44. if (params.deptId) {
  45. this.deptId = params.deptId;
  46. this.title = params.title;
  47. this.loading = true;
  48. this.getdata();
  49. this.getlat();
  50. }
  51. },
  52. methods: {
  53. getdata() {
  54. this.$http("/jmis-riskassess/mine/getXYByDeptId", "GET", {
  55. deptId: this.deptId,
  56. })
  57. .then((res) => {
  58. if (res.statusCode == 200) {
  59. this.mlat = res.data.x;
  60. this.mlng = res.data.y;
  61. this.$nextTick(() => {
  62. this._mapContext = uni.createMapContext("map", this);
  63. // 仅调用初始化,才会触发 on.("markerClusterCreate", (e) => {})
  64. this._mapContext.initMarkerCluster({
  65. enableDefaultStyle: false,
  66. zoomOnClick: true,
  67. gridSize: 60,
  68. complete(res) {
  69. console.log("initMarkerCluster", res);
  70. },
  71. });
  72. this._mapContext.on("markerClusterCreate", (e) => {
  73. console.log("markerClusterCreate", e);
  74. });
  75. this.addMarkers();
  76. });
  77. }
  78. })
  79. .catch(() => {
  80. this.$refs.uToast.show({
  81. type: "error",
  82. icon: false,
  83. message: "服务器异常!",
  84. });
  85. });
  86. },
  87. getlat() {
  88. let that = this;
  89. uni.getLocation({
  90. type: "wgs84", //返回可以用于uni.openLocation的经纬度
  91. success: function (res) {
  92. that.lat = res.latitude;
  93. that.lng = res.longitude;
  94. },
  95. });
  96. },
  97. addMarkers() {
  98. this.$nextTick(() => {
  99. // const positions = [
  100. // {
  101. // latitude: this.mlat,
  102. // longitude: this.mlng,
  103. // },
  104. // ];
  105. const markers = [
  106. {
  107. id: 0,
  108. iconPath: "/static/map2.png",
  109. width: 150,
  110. height: 150,
  111. latitude: this.mlat,
  112. longitude: this.mlng,
  113. joinCluster: true, // 指定了该参数才会参与聚合
  114. label: {
  115. width: 50,
  116. height: 30,
  117. borderWidth: 1,
  118. borderRadius: 10,
  119. bgColor: "#000",
  120. content: this.title ? this.title : "",
  121. },
  122. },
  123. ];
  124. // positions.forEach((p, i) => {
  125. // markers.push(
  126. // Object.assign(
  127. // {},
  128. // p
  129. // )
  130. // );
  131. // });
  132. this.markers = markers;
  133. this._mapContext.addMarkers({
  134. markers,
  135. clear: false,
  136. complete(res) {
  137. console.log("addMarkers", res);
  138. },
  139. });
  140. this.loading = false;
  141. });
  142. },
  143. // 打开的点击事件,传经纬度和地点名
  144. gomapApp() {
  145. let latitude = this.lat;
  146. let longitude = this.lng;
  147. let mlat = this.mlat;
  148. let mlng = this.mlng;
  149. let name = this.title;
  150. let url = "";
  151. let webUrl = "";
  152. if (plus.os.name == "Android") {
  153. //判断是安卓端
  154. plus.nativeUI.actionSheet(
  155. {
  156. //选择菜单
  157. title: "选择地图应用",
  158. cancel: "取消",
  159. buttons: [
  160. // { title: "腾讯地图" },
  161. { title: "百度地图" },
  162. { title: "高德地图" },
  163. ],
  164. },
  165. function (e) {
  166. switch (e.index) {
  167. //下面是拼接url,不同系统以及不同地图都有不同的拼接字段
  168. // case 1:
  169. // //注意referer=xxx的xxx替换成你在腾讯地图开发平台申请的key
  170. // url = `qqmap://map/geocoder?coord=${latitude},${longitude}&referer=xxx`;
  171. // webUrl = `https://apis.map.qq.com/uri/v1/marker?marker=coord:${latitude},${longitude}&title=${name}&addr=${name}&referer=xxx`;
  172. // break;
  173. case 1:
  174. // url = `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`;
  175. url = `baidumap://map/direction?destination=${mlat},${mlng}&mode=driving&src=yourAppName|yourAppName&coord_type=bd09ll&name=${name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`;
  176. webUrl = `http://api.map.baidu.com/marker?location=${mlat},${mlng}&title=${name}&content=${name}&output=html&src=webapp.baidu.openAPIdemo`;
  177. break;
  178. case 2:
  179. url = `amapuri://route/plan?sourceApplication=maxuslife&sid=A&slat=${latitude}&slon=${longitude}&sname=起始地点&did=B&dlat=${mlat}&dlon=${mlng}&dname=${name}&dev=0&t=0`;
  180. // url = `androidamap://viewMap?sourceApplication=appname&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`;
  181. webUrl = `https://uri.amap.com/marker?position=${mlat},${mlng}&name=${name}&src=mypage&coordinate=gaode`;
  182. break;
  183. default:
  184. break;
  185. }
  186. if (url != "") {
  187. url = encodeURI(url);
  188. // console.log(url, '地址')
  189. // 打开 app 导航
  190. plus.runtime.openURL(url, (err) => {
  191. // 失败回到
  192. // 毕竟用户可能没有安装app但一定安装的有浏览器
  193. // 如果失败则说明未安装 直接 打开网页版进行导航
  194. let chooseMap = "";
  195. if (e.index == 1) {
  196. chooseMap = "百度地图";
  197. } else if (e.index == 2) {
  198. chooseMap = "高德地图";
  199. } else {
  200. chooseMap = "腾讯地图";
  201. }
  202. uni.showModal({
  203. title: "提示",
  204. content:
  205. "检测到您本机暂未安装" +
  206. chooseMap +
  207. "应用,是否要选择使用浏览器打开?",
  208. confirmText: "确定", //确定文本的文字
  209. cancelText: "取消", //确定文本的文字
  210. showCancel: true, //没有取消按钮的弹框
  211. success: function (res) {
  212. if (res.confirm) {
  213. plus.runtime.openURL(webUrl);
  214. } else if (res.cancel) {
  215. // plus.nativeUI.alert("本机未安装指定的地图应用");
  216. }
  217. },
  218. });
  219. });
  220. }
  221. }
  222. );
  223. } else {
  224. // iOS上获取本机是否安装了百度高德地图,需要在manifest里配置
  225. // 在manifest.json文件app-plus->distribute->apple->urlschemewhitelist节点下添加
  226. //(如urlschemewhitelist:["iosamap","baidumap"])
  227. plus.nativeUI.actionSheet(
  228. {
  229. title: "选择地图应用",
  230. cancel: "取消",
  231. buttons: [
  232. // { title: "腾讯地图" },
  233. { title: "百度地图" },
  234. { title: "高德地图" },
  235. ],
  236. },
  237. function (e) {
  238. switch (e.index) {
  239. // case 1:
  240. // url = `qqmap://map/geocoder?coord=${latitude},${longitude}&referer=xxx`;
  241. // break;
  242. case 1:
  243. url = `baidumap://map/marker?location=${mlat},${mlng}&title=${name}&content=${name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`;
  244. webUrl = `http://api.map.baidu.com/marker?location=${mlat},${mlng}&title=${name}&content=${name}&output=html&src=webapp.baidu.openAPIdemo`;
  245. break;
  246. case 2:
  247. url = `iosamap://viewMap?sourceApplication=applicationName&poiname=${name}&lat=${mlat}&lon=${mlng}&dev=0`;
  248. webUrl = `https://uri.amap.com/marker?position=${mlng},${mlat}&name=${name}&src=mypage&coordinate=gaode`;
  249. break;
  250. default:
  251. break;
  252. }
  253. if (url != "") {
  254. url = encodeURI(url);
  255. // console.log(url, '地址')
  256. // 打开 app 导航
  257. plus.runtime.openURL(url, (err) => {
  258. // 失败回到
  259. // 毕竟用户可能没有安装app但一定安装的有浏览器
  260. // 如果失败则说明未安装 直接 打开网页版进行导航
  261. let chooseMap = "";
  262. if (e.index == 1) {
  263. chooseMap = "百度地图";
  264. } else if (e.index == 2) {
  265. chooseMap = "高德地图";
  266. } else {
  267. chooseMap = "腾讯地图";
  268. }
  269. uni.showModal({
  270. title: "提示",
  271. content:
  272. "检测到您本机暂未安装" +
  273. chooseMap +
  274. "应用,是否要选择使用浏览器打开?",
  275. confirmText: "确定", //确定文本的文字
  276. cancelText: "取消", //确定文本的文字
  277. showCancel: true, //没有取消按钮的弹框
  278. success: function (res) {
  279. if (res.confirm) {
  280. plus.runtime.openURL(webUrl);
  281. } else if (res.cancel) {
  282. // plus.nativeUI.alert("本机未安装指定的地图应用");
  283. }
  284. },
  285. });
  286. });
  287. }
  288. }
  289. );
  290. }
  291. },
  292. },
  293. };
  294. </script>
  295. <style scoped>
  296. .content {
  297. width: 100%;
  298. }
  299. .map {
  300. width: 100%;
  301. height: 80vh;
  302. }
  303. .button {
  304. width: 95%;
  305. border-radius: 20rpx;
  306. }
  307. .loading {
  308. width: 100%;
  309. height: 100%;
  310. display: flex;
  311. justify-content: center;
  312. align-items: center;
  313. position: absolute;
  314. background-color: #cccccc88;
  315. top: 0;
  316. left: 0;
  317. z-index: 1;
  318. }
  319. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/548628
推荐阅读