Vue中使用高德地圖,實現周邊搜索


網上看了很多Vue中高德地圖的Demo,感覺零零碎碎的,有些說到一半就不知道怎么回事了。在此記錄下(這是我遇到的情況,如有其他坑,請自行踩坑)

忘了說了  這是基於element ui 的菜單欄做的 ,如不需要,自行剔除相關代碼樣式,重新寫個滑塊導航

一、安裝npm

npm i @amap/amap-jsapi-loader --save

二、引入

Script 引入
 
import AMapLoader from "@amap/amap-jsapi-loader";
let AMap = null;
 
三、代碼階段
 
這個代碼基本完成了周邊的類型搜索、標注;只是對搜索類型的還沒做,這個代碼邏輯看懂了,類型搜索很簡單了,我也是看了很多例子,零零碎碎的總結出來的代碼
 
Html Demo:
 1     <el-menu
 2       :default-active="activeIndex"
 3       class="el-menu-demo"
 4       mode="horizontal"
 5       @select="handleSelect"
 6     >
 7       <el-menu-item index="1">交通</el-menu-item>
 8       <el-menu-item index="2">娛樂</el-menu-item>
 9       <el-menu-item index="3">商場</el-menu-item>
10       <el-menu-item index="4">學校</el-menu-item>
11       <el-menu-item index="5">醫療</el-menu-item>
12     </el-menu>
13     <div class="album" id="album"></div>
14   </div>
View Code

JS Demo:

 1 export default {
 2   created() {
 3     this.initAMap();
 4   },
 5   data() {
 6     return {
 7       activeIndex: "1",
 8     };
 9   },
10   mounted() {},
11   methods: {
12     // 周邊搜索
13     searchNear(mapNew) {
14       mapNew.clearMap();
15       let placeSearch = new AMap.PlaceSearch({
16         // city: '', // 興趣點城市
17         citylimit: true, // 是否強制在設置的城市內搜索,默認false
18         type: "地鐵", // 興趣點類別,用‘|’分隔,默認:'餐飲服務|商務住宅|生活服務'
19         pageSize: 20, // 每頁條數,默認10,范圍1-50
20         pageIndex: 1, // 頁碼
21         extensions: "all", // 默認base,返回基本地址信息;all:返回詳細信息
22         // map: AMap, // 地圖實例,可選
23         // panel: 'panel', // 結果列表的id容器,可選
24         autoFitView: true, // 是否自動調整地圖視野到marker點都處於可見范圍
25       });
26 
27       let keywords = "", // 關鍵字
28         position = [104.070081, 30.581108], // 中心點經緯度
29         radius = 2000; // 搜索半徑 0-50000
30       placeSearch.searchNearBy(
31         keywords,
32         position,
33         radius,
34         function (status, result) {
35           console.log(mapNew, "地圖實列");
36           // var icon = new AMap.Icon({
37           //   size: new AMap.Size(10, 10), // 圖標尺寸
38           //   image: iconquyu, // Icon的圖像
39           //   imageOffset: new AMap.Pixel(0, -60), // 圖像相對展示區域的偏移量,適於雪碧圖等
40           //   imageSize: new AMap.Size(40, 50), // 根據所設置的大小拉伸或壓縮圖片
41           // });
42           // console.log(icon,"圖片");
43           new AMap.Marker({
44               position: [
45                 104.070081,
46                 30.581108,
47               ], //不同標記點的經緯度s
48               // icon: "http://store.is.autonavi.com/showpic/2f754f895d410592bdf46eeddc379bee",//中心點icon
49               title: 'aaa',
50               clickable:true,
51               // content:result.poiList.pois[i].name,
52               // text:result.poiList.pois[i].name,
53               // offset:[0,50],
54               map: mapNew,
55             });
56           for (let i = 0; i < result.poiList.pois.length; i++) {
57             new AMap.Marker({
58               position: [
59                 result.poiList.pois[i].location.lng,
60                 result.poiList.pois[i].location.lat,
61               ], //不同標記點的經緯度 
62               // icon: "http://store.is.autonavi.com/showpic/2f754f895d410592bdf46eeddc379bee",
63               title: result.poiList.pois[i].name,
64               clickable:true,
65               // content:result.poiList.pois[i].name,
66               // text:result.poiList.pois[i].name,
67               // offset:[0,50],
68               map: mapNew,
69             });
70           }
71           console.log(status, result, "結果"); //這個結果可以看出周邊的很多信息列表,根據結果做很多事,如果要翻頁,請移步官網 在此如有需要  附鏈接https://lbs.amap.com/api/webservice/guide/api/newpoisearch
72         }
73       );
74     },
75     handleSelect(key, keyPath) {
76       console.log(key, keyPath);
77     },
78     initAMap() {
79       let that = this;
80       AMapLoader.load({
81         key: "0a3647329c1fceff9e14bcd1a27f4f0d",
82         version: "2.0",
83         plugins: ["AMap.PlaceSearch", "AMap.Geolocation"],
84       })
85         .then((map) => {
86           AMap = map;
87           // 其他功能同h5
88           var mapNew = new AMap.Map("album", {
89             center: [104.070081, 30.581108], // 地圖中心點坐標
90             zoom: 15, // 地圖縮放級別
91           });
92           that.searchNear(mapNew);
93         })
94         .catch((e) => {
95           console.log("高德地圖加載錯誤", e);
96         });
97     },
98   },
99 };
View Code

Css Demo:

 1 .album {
 2   width: 824px;
 3   height: 532px;
 4   border-radius: 4px;
 5   position: relative;
 6 }
 7 .el-menu.el-menu--horizontal {
 8   border-bottom: none;
 9 }
10 .el-menu--horizontal > .el-menu-item.is-active {
11   border-bottom: none;
12   color: #409eff !important;
13 }
14 .el-menu--horizontal > .el-menu-item {
15   border-bottom: none;
16 }
View Code

代碼看完,如對標記不懂的,移步:https://lbs.amap.com/api/javascript-api/reference/overlay#marker

對路線規划不懂的,移步:https://lbs.amap.com/api/javascript-api/guide/services/navigation

最后是我對每個功能代碼期望的 效果圖,順便吐槽下:對那些沒得效果圖的,發出來干嘛啊  哈哈哈哈

   key  自行去搜索高德地圖Api, 到官網的控制台注冊申請,創建個測試項目就有了

   對了   如果對其中參數還不明白的  ,多看官網說明,理解就很簡單了(再附一句,代碼里面有很多測試代碼已注釋,基本沒啥用,忘刪了)

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM