在vue中高德地圖引入和軌跡的繪製的實現
阿新 • • 發佈:2020-11-19
高德地圖引入和軌跡的繪製
1.第一步
vue中使用cdn引入高德地圖,並在main.js中進行全域性配置。(百度上有高德地圖引入與配置方法,這裡就不詳細介紹);
1) npm install vue-amap --save
2)
import VueAMap from ‘vue-amap' Vue.use(VueAMap); VueAMap.initAMapApiLoader({ key: ‘********************',//自己在高德地圖平臺上的key值 plugin: [‘AMap.Autocomplete', ‘AMap.PlaceSearch', ‘AMap.Scale', ‘AMap.OverView', ‘AMap.ToolBar', ‘AMap.MapType', ‘AMap.PolyEditor', ‘AMap.CircleEditor',‘AMap.ControlBar',‘AMap.MouseTool',‘AMap.GeometryUtil',‘AMap.DistrictSearch'],//需要的高德地圖外掛,需要什麼外掛新增什麼外掛(這裡只是其中一部分) // 預設高德 sdk 版本為 1.4.4 v: ‘1.4.4', uiVersion:‘1.0.11' });
2.第二步
//引入地圖,這時地圖已經可以在頁面上顯示了。 this.map = new AMap.Map('themap', { resizeEnable: true, center:[121.716284,29.888144],//這是地圖的中心點 zoom: 18,//地圖的層級 layers: [ new AMap.TileLayer, this.imageLayer //這是我在地圖上繪製自己的圖層用的方法,可去掉。 ] });
//引入Marker,繪製點標記 this.marker = new AMap.Marker({ map: this.map, position: this.firstArr, icon: pic,//這裡是我要的圖片 });
//繪製軌跡 this.polyline = new AMap.Polyline({ map: this.map, path: this.lineArr, // 這裡是軌跡的座標拼成的陣列 showDir: true, strokeColor: "#28F", //線顏色 // strokeOpacity: 1, //線透明度 strokeWeight: 6 //線寬 // strokeStyle: "solid" //線樣式 }); var passedPolyline = new AMap.Polyline({ map: this.map, strokeColor: "#AF5", //線顏色 //path: this.lineArr, // strokeOpacity: 1, //線透明度 strokeWeight: 6 //線寬 // strokeStyle: "solid" //線樣式 }); this.marker.on("moving", function(e) { passedPolyline.setPath(e.passedPath); }); this.map.setFitView();//自動調整到合適的位置
以上就是軌跡繪製的整個過程
擴充套件
要想在自己的地圖上繪製圖層,可以用上面用到的imageLayer
this.imageLayer = new AMap.ImageLayer({ url:tupian , //這裡是自己的圖片 bounds: new AMap.Bounds( [121.71105271149695,29.885719370176783],//左下角的座標 [121.72236765648086,29.891597442759533],//右上角的座標 ), zooms: [15, 18] //這裡是在15到18的範圍內顯示 });
這裡要提示一下,放的圖片一定要根據地圖的方向。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援碼農教程。