修改webpack.base.config.js(必须配不然会报错)

  1. externals: {
  2. 'AMap': 'AMap'
  3. }

注意:重新运行一下

引入

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1.0">
  6. <title>首页</title>
  7. <script src="https://webapi.amap.com/maps?v=1.3&key=63343cf54c48885db27a9f563ec0a46f"></script>
  8. </head>

注意:必须放在head里 不然容易报错

使用

初始化地图

  1. <!--
  2. * @Description: In User Settings Edit
  3. * @Author: your name
  4. * @Date: 2019-08-13 10:17:52
  5. * @LastEditTime: 2019-08-16 15:22:00
  6. * @LastEditors: Please set LastEditors
  7. -->
  8. <template>
  9. <div>
  10. <div class="map" id="mapContainer"></div>
  11. </div>
  12. </template>
  13. <script>
  14. import AMap from "AMap";
  15. var map, driving;
  16. export default {
  17. data() {
  18. return {
  19. };
  20. },
  21. beforeRouteEnter(to, from, next) {
  22. next();
  23. },
  24. methods: {
  25. initMap() {
  26. let _this = this;
  27. map = new AMap.Map("mapContainer", {
  28. zoom: 10,
  29. resizeEnable: true
  30. });
  31. // 使用插件
  32. AMap.plugin(["AMap.Driving", "AMap.Geolocation"], function() {
  33. _this.location();
  34. if(_this.$route.query.hasOwnProperty('lng')){
  35. map.clearMap();
  36. _this.drivingLine(_this.$route.query.lng, _this.$route.query.lat)
  37. }else{
  38. _this.getMapIndex();
  39. }
  40. });
  41. }
  42. },
  43. mounted() {
  44. this.initMap();
  45. }
  46. };
  47. </script>
  48. <style lang="less">
  49. // 修改地图组件
  50. .amap-geolocation-con {
  51. bottom: 55px !important;
  52. }
  53. .map {
  54. width: 100%;
  55. height: 100%;
  56. }
  57. </style>

使用插件

初始化是就把插件也初始化 这样在外面也可以使用这个插件 不必在回调函数内

  1. // 使用插件
  2. AMap.plugin(["AMap.Driving", "AMap.Geolocation"], function() {
  3. _this.location();
  4. if(_this.$route.query.hasOwnProperty('lng')){
  5. map.clearMap();
  6. _this.drivingLine(_this.$route.query.lng, _this.$route.query.lat)
  7. }else{
  8. _this.getMapIndex();
  9. }
  10. });

路线规划

  1. map.clearMap(); //清除地图所有标记
  2. let _this = this;
  3. // 规划路线必须要清除前一次的 否则路线会重叠多个
  4. if (driving) { //一定要判断要不然会报错
  5. driving.clear();// 清除历史规划路线
  6. }
  7. driving = new AMap.Driving({
  8. map: map
  9. });
  10. driving.search(map.getCenter(), [lng, lat], function(status, result) {
  11. if (status === "complete") {
  12. console.log("绘制驾车路线完成");
  13. driving.searchOnAMAP({
  14. origin:result.origin,
  15. destination:result.destination
  16. });
  17. _this.showInfo = false;
  18. } else {
  19. console.log("获取驾车数据失败:" + result);
  20. }
  21. });

完整代码

有定位,标点,路线规划功能 其他要使用可以直接看原生高德地图文档

  1. <!--
  2. * @Description: In User Settings Edit
  3. * @Author: your name
  4. * @Date: 2019-08-13 10:17:52
  5. * @LastEditTime: 2019-08-16 15:22:00
  6. * @LastEditors: Please set LastEditors
  7. -->
  8. <template>
  9. <div class="map">
  10. <div class="map-search">
  11. <input
  12. type="search"
  13. id="keywords"
  14. placeholder="搜索联盟成员"
  15. v-model="keyword_name"
  16. v-on:keydown.13="search"
  17. />
  18. </div>
  19. <div class="map" id="mapContainer"></div>
  20. <ul class="cate">
  21. <li @click="tab_search('')">
  22. <img src="/static/images/all.png" alt="">
  23. </li>
  24. <li v-for="(item, index) in cate_list" :key="index" @click="tab_search(item.cate_name)">
  25. <img :src="item.cate_icon" alt="">
  26. </li>
  27. </ul>
  28. <div v-if="showWindow" :class="showInfo?'search-info slideUp':'search-info slideDown'">
  29. <div class="info">
  30. <div class="desc">
  31. <h1>
  32. {{infoWindow.title}}
  33. <i @click="toList">更多</i>
  34. </h1>
  35. <p>{{infoWindow.desc}}</p>
  36. </div>
  37. <div
  38. class="address"
  39. ref="address"
  40. @click="drivingLine(infoWindow.lng, infoWindow.lat)"
  41. >
  42. <div class="position">
  43. <img src="../../../assets/images/fitMapList/position1.png" alt />&nbsp;
  44. <span>{{infoWindow.distance}}km</span>
  45. </div>
  46. </div>
  47. <ul class="list">
  48. <li>
  49. <p>{{infoWindow.address}}</p>
  50. <span></span>
  51. <a :href="'tel://' + infoWindow.tel">
  52. <img src="../../../assets/images/fitMapList/phone.png" alt />
  53. </a>
  54. </li>
  55. </ul>
  56. </div>
  57. <div class="border" @click="openInfo"></div>
  58. <div class="close" @click="closeInfo"></div>
  59. </div>
  60. </div>
  61. </template>
  62. <script>
  63. import AMap from "AMap";
  64. var map, driving;
  65. export default {
  66. data() {
  67. return {
  68. showWindow: false,
  69. showInfo: false,
  70. position: [],
  71. markers: [],
  72. infoWindow: {},
  73. keyword_name: "",
  74. cate_list: []
  75. };
  76. },
  77. beforeRouteEnter(to, from, next) {
  78. next();
  79. },
  80. methods: {
  81. closeInfo() {
  82. this.showInfo = false;
  83. },
  84. openInfo() {
  85. this.showInfo = true;
  86. },
  87. toList() {
  88. this.$router.push({path: '/mapmarklist'})
  89. },
  90. getMapIndex() {
  91. let _this = this;
  92. this.$get("/fmmap/map/index").then(res => {
  93. this.setMarkers(res.data.anchor_list);
  94. });
  95. },
  96. setMarkers(markers) {
  97. let _this = this;
  98. markers.forEach(function(marker, index) {
  99. let center = map.getCenter();
  100. console.log(center)
  101. let dis = Math.round(center.distance([marker.lng, marker.lat]));
  102. marker.distance = (dis / 1000).toFixed(1);
  103. var marker = new AMap.Marker({
  104. map: map,
  105. icon: "/static/images/position.png",
  106. position: [marker.lng, marker.lat],
  107. info: marker
  108. });
  109. marker.on("click", function(e) {
  110. _this.showInfo = true;
  111. _this.showWindow = true;
  112. _this.infoWindow = e.target.G.info;
  113. });
  114. });
  115. map.setFitView();
  116. },
  117. initMap() {
  118. let _this = this;
  119. map = new AMap.Map("mapContainer", {
  120. zoom: 10,
  121. resizeEnable: true
  122. });
  123. AMap.plugin(["AMap.Driving", "AMap.Geolocation"], function() {
  124. _this.location();
  125. if(_this.$route.query.hasOwnProperty('lng')){
  126. map.clearMap();
  127. _this.drivingLine(_this.$route.query.lng, _this.$route.query.lat)
  128. }else{
  129. _this.getMapIndex();
  130. }
  131. });
  132. },
  133. location() {
  134. // 定位
  135. var geolocation = new AMap.Geolocation({
  136. enableHighAccuracy: true, //是否使用高精度定位,默认:true
  137. timeout: 10000, //超过10秒后停止定位,默认:无穷大
  138. maximumAge: 0, //定位结果缓存0毫秒,默认:0
  139. convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
  140. showButton: true, //显示定位按钮,默认:true
  141. buttonPosition: "LB", //定位按钮停靠位置,默认:'LB',左下角
  142. buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
  143. showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
  144. showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
  145. panToLocation: false, //定位成功后将定位到的位置作为地图中心点,默认:true
  146. zoomToAccuracy: false //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
  147. });
  148. map.addControl(geolocation);
  149. geolocation.getCurrentPosition();
  150. AMap.event.addListener(geolocation, "complete", function(result) {
  151. console.log("定位成功");
  152. }); //返回定位信息
  153. AMap.event.addListener(geolocation, "error", function(error) {
  154. console.log(JSON.stringify(error));
  155. });
  156. },
  157. drivingLine(lng, lat) {
  158. map.clearMap();
  159. let _this = this;
  160. if (driving) {
  161. // 清除历史规划路线
  162. driving.clear();
  163. }
  164. driving = new AMap.Driving({
  165. map: map
  166. });
  167. driving.search(map.getCenter(), [lng, lat], function(status, result) {
  168. if (status === "complete") {
  169. console.log("绘制驾车路线完成");
  170. driving.searchOnAMAP({
  171. origin:result.origin,
  172. destination:result.destination
  173. });
  174. _this.showInfo = false;
  175. } else {
  176. console.log("获取驾车数据失败:" + result);
  177. }
  178. });
  179. },
  180. tab_search(name) {
  181. this.keyword_name = name;
  182. this.search();
  183. },
  184. search() {
  185. map.clearMap();
  186. this.showWindow = false;
  187. this.$get("/fmmap/map/anchor/search", {
  188. lng: map.getCenter().lng,
  189. lat: map.getCenter().lat,
  190. keyword_name: this.keyword_name,
  191. uniacid: 1
  192. }).then(res => {
  193. this.setMarkers(res.data.anchor_list);
  194. });
  195. },
  196. getCate() {
  197. this.$get('/fmmap/map/cate/list').then(res => {
  198. if(res.data.length == 0) return;
  199. res.data.map(item => {
  200. item.cate_anchor_icon = this.axios.defaults.baseUrl + item.cate_anchor_icon;
  201. })
  202. this.cate_list = res.data;
  203. })
  204. }
  205. },
  206. mounted() {
  207. this.initMap();
  208. this.getCate();
  209. }
  210. };
  211. </script>
  212. <style lang="less">
  213. // 修改地图组件
  214. .amap-geolocation-con {
  215. bottom: 55px !important;
  216. }
  217. .map {
  218. width: 100%;
  219. height: 100%;
  220. .map-search {
  221. position: fixed;
  222. top: 15px;
  223. width: 100%;
  224. height: 45px;
  225. text-align: center;
  226. z-index: 9999;
  227. input {
  228. box-sizing: border-box;
  229. width: 90%;
  230. height: 45px;
  231. border-radius: 8px;
  232. border: 0;
  233. outline: none;
  234. margin: 0 auto;
  235. font-size: 14px;
  236. color: #333;
  237. font-family: "PingFangSC-Regular";
  238. background: url("../../../assets/images/fitMapList/search.png") no-repeat
  239. 10px center #fff;
  240. background-size: 18px 18px;
  241. padding: 5px 8px 5px 32px;
  242. }
  243. ul {
  244. width: 45px;
  245. box-sizing: border-box;
  246. margin-top: 10px;
  247. position: absolute;
  248. right: 10px;
  249. li {
  250. width: 40px;
  251. height: 40px;
  252. // border: 1px solid #e4e4e4;
  253. margin-bottom: 4px;
  254. border-radius: 50%;
  255. img {
  256. width: 40px;
  257. height: 40px;
  258. }
  259. }
  260. }
  261. }
  262. .search-info {
  263. width: 97%;
  264. height: 50%;
  265. background: #fff;
  266. margin: 0 auto;
  267. position: fixed;
  268. bottom: -45%;
  269. left: 50%;
  270. transform: translateX(-50%);
  271. z-index: 9999;
  272. &.slideUp {
  273. animation: slideUp 0.4s forwards;
  274. .close {
  275. display: block;
  276. }
  277. }
  278. &.slideDown {
  279. animation: slideDown 0.4s forwards;
  280. }
  281. .close {
  282. position: absolute;
  283. right: 12px;
  284. top: 12px;
  285. width: 17px;
  286. height: 17px;
  287. background: url("../../../assets/images/fitMapList/cloase.png") no-repeat
  288. center center;
  289. display: none;
  290. }
  291. .border {
  292. width: 45px;
  293. border: 4px solid #eceaea;
  294. position: absolute;
  295. top: 10px;
  296. left: 50%;
  297. transform: translateX(-50%);
  298. border-radius: 10px;
  299. }
  300. .info {
  301. margin-top: 35px;
  302. width: 100%;
  303. overflow-y: scroll;
  304. height: 90%;
  305. .desc {
  306. padding: 10px;
  307. border-bottom: 1px solid #e4e4e4;
  308. padding-bottom: 6px;
  309. h1 {
  310. font-size: 0.34rem;
  311. color: #333;
  312. font-weight: 500;
  313. margin-bottom: 0.2rem;
  314. position: relative;
  315. width: 100%;
  316. height: auto;
  317. padding-right: 0.5rem;
  318. i {
  319. position: absolute;
  320. top: 0.06rem;
  321. right: 0;
  322. font-size: 0.24rem;
  323. cursor: pointer;
  324. }
  325. }
  326. .grade {
  327. font-size: 0;
  328. margin: 5px 0;
  329. i {
  330. display: inline-block;
  331. width: 12px;
  332. height: 12px;
  333. background: url("../../../assets/images/fitMapList/start2.png")
  334. no-repeat center center;
  335. background-size: 12px 12px;
  336. margin-right: 2px;
  337. &.active {
  338. background: url("../../../assets/images/fitMapList/start1.png")
  339. no-repeat center center;
  340. background-size: 12px 12px;
  341. }
  342. }
  343. }
  344. p {
  345. font-size: 0.26rem;
  346. color: #333;
  347. }
  348. }
  349. .address {
  350. padding: 10px;
  351. border-bottom: 1px solid #e4e4e4;
  352. .position {
  353. color: #000;
  354. font-size: 15px;
  355. width: 100%;
  356. height: 45px;
  357. border: 1px solid #e4e4e4;
  358. text-align: center;
  359. line-height: 45px;
  360. margin: 12px auto;
  361. img {
  362. display: inline-block;
  363. width: 20px;
  364. height: 20px;
  365. position: relative;
  366. top: 28%;
  367. }
  368. }
  369. .label {
  370. font-size: 0;
  371. span {
  372. font-size: 12px;
  373. color: #999;
  374. padding: 4px 6px;
  375. border: 1px solid #979797;
  376. border-radius: 4px;
  377. margin-right: 6px;
  378. }
  379. }
  380. }
  381. .list {
  382. padding: 10px;
  383. border-bottom: 1px solid #e4e4e4;
  384. li {
  385. line-height: 40px;
  386. background: url("../../../assets/images/fitMapList/position.png")
  387. no-repeat left center;
  388. background-size: 11px 16px;
  389. padding-left: 20px;
  390. font-size: 14px;
  391. display: flex;
  392. align-items: center;
  393. position: relative;
  394. p {
  395. width: 82.2%;
  396. white-space: nowrap;
  397. overflow: hidden;
  398. text-overflow: ellipsis;
  399. }
  400. span {
  401. width: 1px;
  402. height: 22px;
  403. background: #e4e4e4;
  404. display: block;
  405. }
  406. a {
  407. display: block;
  408. width: 0.6rem;
  409. height: 0.6rem;
  410. display: flex;
  411. position: absolute;
  412. right: 0;
  413. top: 0;
  414. padding-top: 0.2rem;
  415. img {
  416. width: 0.4rem;
  417. height: 0.4rem;
  418. align-self: center;
  419. }
  420. }
  421. }
  422. }
  423. }
  424. }
  425. }
  426. // 图标样式
  427. .cate{
  428. position: fixed;
  429. right: .4rem;
  430. top: 1.5rem;
  431. z-index: 9999;
  432. li{
  433. width: 0.8rem;
  434. height: 0.8rem;
  435. margin-top: .2rem;
  436. border-radius: 50%;
  437. img{
  438. width: 100%;
  439. }
  440. }
  441. }
  442. @keyframes slideDown {
  443. 0% {
  444. bottom: 0;
  445. }
  446. 100% {
  447. bottom: -45%;
  448. }
  449. }
  450. @keyframes slideUp {
  451. 0% {
  452. bottom: -45%;
  453. }
  454. 100% {
  455. bottom: 0;
  456. }
  457. }
  458. </style>