mapNew - 好用参考版.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <view>
  3. <map id="test_map" ref="map1" style="width: 100%; height: 600px" :show-location="true"
  4. :longitude="map.longitude" :latitude="map.latitude" :markers="markers" :polyline="polyline"
  5. show-compass="ture" show-location="true" :controls="controls" @tap="tapMap">
  6. </map>
  7. <button @click="dingwei">定位到"北京西站"</button>
  8. <button @click="dingwei2">ref 定位到"北京西站"</button>
  9. <button @click="chooseLocation">选择地点</button>
  10. <button @click="getDriverLine">开始导航</button>
  11. <button @click="openNavigation">打开app</button>
  12. <button @click="openNavigation">规划路线</button>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. props: {
  18. getLocation: {
  19. type: Object,
  20. default: null,
  21. },
  22. },
  23. data() {
  24. return {
  25. mapContext: {},
  26. map: {
  27. longitude: "", //经度
  28. latitude: "", //纬度
  29. },
  30. form: {
  31. name: "",
  32. area: "",
  33. address: "",
  34. location: "",
  35. },
  36. runningRoute: "",
  37. polyline: [{
  38. // 每个点的经纬度
  39. points: [{
  40. longitude: 118.769025,
  41. latitude: 31.976056,
  42. },
  43. {
  44. longitude: 118.77464,
  45. latitude: 31.974362,
  46. },
  47. {
  48. longitude: 118.774039,
  49. latitude: 31.97631,
  50. },
  51. ],
  52. // 颜色
  53. color: "#000",
  54. // 宽度
  55. width: 10,
  56. dottedLine: true,
  57. arrowLine: true,
  58. colorList: true,
  59. }, ],
  60. controls: [
  61. // {
  62. // //在地图上显示控件,控件不随着地图移动
  63. // id: 1, //控件id
  64. // iconPath: "/static/c1.png", //显示的图标
  65. // position: {
  66. // //控件在地图的位置
  67. // left: 15,
  68. // top: 15,
  69. // width: 50,
  70. // height: 50,
  71. // },
  72. // },
  73. ],
  74. markers: [
  75. // {
  76. // longitude: 118.769032,
  77. // latitude: 31.975258,
  78. // iconPath: "/static/c1.png",
  79. // },
  80. ],
  81. };
  82. },
  83. created() {
  84. let _this = this;
  85. if (this.getLocation != null) {
  86. console.log("当前位置", this.getLocation);
  87. _this.map.longitude = this.getLocation.longitude;
  88. _this.map.latitude = this.getLocation.latitude;
  89. _this.getDriverLine();
  90. return;
  91. }
  92. },
  93. mounted() {
  94. this.mapContext = uni.createMapContext("test_map", this);
  95. //起点
  96. // this.addMarler();
  97. // 终点
  98. // this.addEndMarler();
  99. },
  100. methods: {
  101. // 导航 会打开导航菜单供用户选择,都是打开内置地图
  102. openNavigation() {
  103. //方法1
  104. let longitude = xxxxx;
  105. let latitude = xxxxxx;
  106. let name = '想要去的地方'
  107. let url = ""; // app url
  108. let webUrl = ""; // web url 用来为用户未安装导航软件时打开浏览器所使用url
  109. plus.nativeUI.actionSheet({
  110. //选择菜单
  111. title: "选择地图应用",
  112. cancel: "取消",
  113. buttons: [{
  114. title: "高德地图"
  115. }], // 可选的地图类型
  116. },
  117. (e) => {
  118. // 判断用户选择的地图
  119. switch (e.index) {
  120. //下面是拼接url,不同系统以及不同地图都有不同的拼接字段
  121. case 1:
  122. // 安卓
  123. if (plus.os.name == "Android") {
  124. url =
  125. `androidamap://viewMap?sourceApplication=appname&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`;
  126. } else {
  127. url =
  128. `iosamap://viewMap?sourceApplication=applicationName&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`;
  129. }
  130. webUrl =
  131. `https://uri.amap.com/marker?position=${longitude},${latitude}&name=${name}&src=mypage&coordinate=gaode`;
  132. break;
  133. }
  134. // 如果选中
  135. if (url != "") {
  136. url = encodeURI(url);
  137. // 打开 app 导航
  138. plus.runtime.openURL(url, (err) => {
  139. // 失败回到
  140. // 如果失败则说明未安装 直接 打开网页版进行导航
  141. // 毕竟用户可能没有安装app但一定安装的有浏览器
  142. plus.runtime.openURL(webUrl);
  143. });
  144. }
  145. }
  146. );
  147. //方法2
  148. uni.openLocation({
  149. latitude: 31.975258,
  150. longitude: 118.769032,
  151. name: "想去的地方",
  152. success() {
  153. console.log("success");
  154. },
  155. });
  156. },
  157. //路线规划,调用高德地图的api实现规划路线的功能。
  158. getDriverLine() {
  159. const that = this;
  160. //这个key是在高德导航app上申请的web路线规划
  161. const key = "";
  162. //起点坐标
  163. const origin = "xxx,xxx";
  164. //给起点坐标一个图标
  165. this.startingPoint()
  166. //给终点坐标一个图标
  167. this.endPoint()
  168. //终点坐标
  169. const destination = "xxx,xxx";
  170. uni.request({
  171. url: `https://restapi.amap.com/v3/direction/driving?origin=${origin}&destination=${destination}&key=${key}`,
  172. success: (res) => {
  173. console.log(res);
  174. const data = res.data.route;
  175. var points = [];
  176. if (data.paths && data.paths[0] && data.paths[0].steps) {
  177. var steps = data.paths[0].steps;
  178. for (var i = 0; i < steps.length; i++) {
  179. //将每一步的数据放到points数组中
  180. var poLen = steps[i].polyline.split(";");
  181. for (var j = 0; j < poLen.length; j++) {
  182. points.push({
  183. longitude: parseFloat(poLen[j].split(",")[0]),
  184. latitude: parseFloat(poLen[j].split(",")[1]),
  185. });
  186. }
  187. }
  188. console.log(data.paths[0].steps[0].instruction)
  189. //这个是我用来测试的,指的是开始导航之后的要走的第一步路
  190. that.runningRoute = data.paths[0].steps[0].instruction;
  191. console.log('行驶路线-----------', that.runningRoute)
  192. }
  193. //这个就是走的路,下面有几个属性在app上不支持
  194. that.polyline = [{
  195. points: points,
  196. color: "#0091ff",
  197. dottedLine: true,
  198. width: 15,
  199. arrowLine: true,
  200. colorList: true,
  201. }, ];
  202. },
  203. fail: function(res) {
  204. console.log("获取路线失败", res);
  205. },
  206. });
  207. },
  208. // 规划路线的时候给起点一个marker,
  209. startingPoint() {
  210. let point = [{
  211. id: 1,
  212. width: 40,
  213. height: 40,
  214. latitude: xxxx,
  215. longitude: xxxx,
  216. iconPath: "/static/qidian.png",
  217. anchor: {
  218. x: 0.5,
  219. y: 1,
  220. },
  221. }, ];
  222. this.markers = this.markers.concat(point);
  223. },
  224. // 规划路线的时候给终点一个marker,
  225. endPoint() {
  226. let point = [{
  227. id: 2,
  228. width: 40,
  229. height: 40,
  230. latitude: xxxx,
  231. longitude: xxxx,
  232. iconPath: "/static/zhongdian.png",
  233. anchor: {
  234. x: 0.5,
  235. y: 1,
  236. },
  237. }, ];
  238. this.markers = this.markers.concat(point);
  239. },
  240. daohang() {
  241. uni.openLocation({
  242. longitude: parseFloat(118.769025),
  243. latitude: parseFloat(31.976056),
  244. scale: 18,
  245. success: function() {
  246. console.log("success");
  247. },
  248. });
  249. },
  250. //点击之后打点的功能
  251. tapMap(e) {
  252. var that = this;
  253. var id = e.currentTarget.id;
  254. var maps = uni.createMapContext("test_map", this).$getAppMap();
  255. maps.onclick = function(point) {
  256. console.log(point);
  257. point.iconPath = "/static/c1.png";
  258. that.markers = that.markers.concat(point);
  259. uni.showToast({
  260. title: "添加成功",
  261. icon: "none",
  262. });
  263. };
  264. },
  265. //定位当前位置方法1
  266. dingwei() {
  267. this.mapContext.moveToLocation({
  268. //该固定坐标为高德地图拾取,位置:北京西站
  269. latitude: 39.894589,
  270. longitude: 116.32125,
  271. success: function(res) {
  272. console.log("此处无回调!!!本条信息未在控制台打印");
  273. console.log(res);
  274. },
  275. });
  276. },
  277. //定位当前位置方法2
  278. dingwei2() {
  279. console.log(this.$refs.map1);
  280. this.$refs.map1.moveToLocation({
  281. //该固定坐标为高德地图拾取,位置:北京西站
  282. latitude: 39.894589,
  283. longitude: 116.32125,
  284. },
  285. function(res) {
  286. console.log("此处无回调!!!本条信息未在控制台打印");
  287. console.log(res);
  288. }
  289. );
  290. },
  291. //自带选址(这个我没用到)
  292. chooseLocation() {
  293. uni.chooseLocation({
  294. success: (res) => {
  295. console.log(res);
  296. this.form.name = res.name;
  297. this.form.area = res.address;
  298. this.form.location = this.util.formatLocation(
  299. res.longitude,
  300. res.latitude
  301. );
  302. this.setMap(res);
  303. },
  304. });
  305. },
  306. setMap(res) {
  307. this.longitude = res.longitude;
  308. this.latitude = res.latitude;
  309. this.markers[0].longitude = res.longitude;
  310. this.markers[0].latitude = res.latitude;
  311. }
  312. }
  313. }
  314. </script>
  315. <style>
  316. </style>