日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > vue >内容正文

vue

vue 引入高德地图 路线规划

發(fā)布時(shí)間:2023/12/14 vue 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue 引入高德地图 路线规划 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

由于vue-amap不支持路線規(guī)劃,因此不予采納。
效果如圖

  • 在index.html的header中引入
  • <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=e72a9f0cac3b081df05259299454cf1a"></script><!--引入U(xiǎn)I組件庫(kù)(1.0版本) --> <script type="text/javascript" src="https://webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>
  • 配置webpack
    在build\webpack.base.conf.js 中,找到module.exports, 添加如下代碼,記得重啟項(xiàng)目!!!!
  • externals: {AMap: 'AMap',AMapUI: 'AMapUI' },

  • 繪制地圖并規(guī)劃路線(關(guān)鍵代碼)
  • 用來(lái)放地圖

    <div class="page" id="map-container"></div>

    用來(lái)放路線規(guī)劃

    <div id="panel"></div>

    初始化

    this.map = new AMap.Map("map-container", {resizeEnable: true,center: [108.9470386505, 34.2593889736], // 地圖中心點(diǎn)zoom: 16, // 地圖顯示的縮放級(jí)別 });

    公交路線查詢

    new AMap.Transfer({map: this.map,panel: "panel",}).search([{ keyword: "寧波大學(xué)", city: "寧波" },{ keyword: "寧波老外灘", city: "寧波" },],function (status, data) {console.log(data);});
  • 完整代碼(精簡(jiǎn)版)
  • <template><div class="map-content"><div class="page" id="map-container"></div><div id="panel"></div></div> </template> <script> import AMap from "AMap"; import AMapUI from "AMapUI"; export default {name: "Amap",data() {return {map: null,};},mounted() {// 地圖初始化this.map = new AMap.Map("map-container", {resizeEnable: true,center: [108.9470386505, 34.2593889736], // 地圖中心點(diǎn)zoom: 16, // 地圖顯示的縮放級(jí)別});// 'AMap.ToolBar'集成了縮放、平移、定位等功能,'AMap.Scale'展示地圖在當(dāng)前層級(jí)和緯度下的比例尺// 添加需要操作的類AMap.Transfer(公交換乘[不包含地鐵]),AMap.Geolocation(瀏覽器精準(zhǔn)定位)// 公交站點(diǎn)查詢AMap.plugin(["AMap.ToolBar","AMap.Scale","AMap.Transfer","AMap.Geolocation","AMap.StationSearch",],() => {this.map.addControl(new AMap.ToolBar());this.map.addControl(new AMap.Scale());this.map.addControl(new AMap.Transfer());this.map.addControl(new AMap.Geolocation());this.map.addControl(new AMap.StationSearch());});this.getRoute(); // 獲取線路規(guī)劃},methods: {// 1.路線規(guī)劃,不乘坐地鐵2.自行車出行(暫時(shí)不做)3.最快捷模式(綜合出行[包含地鐵]),用1,2,3來(lái)區(qū)分出行方式(goMethod)// 路線規(guī)劃文檔地址https://lbs.amap.com/api/javascript-api/reference/route-search#m_TransferPolicygetRoute() {new AMap.Transfer({map: this.map,panel: "panel",}).search([{ keyword: "寧波大學(xué)", city: "寧波" },{ keyword: "寧波老外灘", city: "寧波" },],function (status, data) {console.log(data);});},}, }; </script> <style scoped> .page {height: calc(100vh - 50px); } .map-content {position: relative; } #panel {position: absolute;top: 0;right: 0; } </style>
  • 完整代碼(定位、公交站點(diǎn)搜索、路線規(guī)劃)
  • <template><div class="map-content"><div class="page" id="map-container"></div><div id="routeInfo"></div></div> </template> <script> import AMap from "AMap"; import AMapUI from "AMapUI"; export default {name: "Amap",components: { AMap, AMapUI },data() {return {map: null,transOptions: {},routeListData: [],stationListData: [],};},props: {sartAndEnd: Array, // 線路導(dǎo)乘起終點(diǎn)經(jīng)緯度},mounted() {// 地圖初始化this.map = new AMap.Map("map-container", {resizeEnable: true,center: [108.9470386505, 34.2593889736], // 地圖中心點(diǎn)zoom: 16, // 地圖顯示的縮放級(jí)別});// 'AMap.ToolBar'集成了縮放、平移、定位等功能,'AMap.Scale'展示地圖在當(dāng)前層級(jí)和緯度下的比例尺// 添加需要操作的類AMap.Transfer(公交換乘[不包含地鐵]),AMap.Geolocation(瀏覽器精準(zhǔn)定位)// 公交站點(diǎn)查詢AMap.plugin(["AMap.ToolBar","AMap.Scale","AMap.Transfer","AMap.Geolocation","AMap.StationSearch",],() => {this.map.addControl(new AMap.ToolBar());this.map.addControl(new AMap.Scale());this.map.addControl(new AMap.Transfer());this.map.addControl(new AMap.Geolocation());this.map.addControl(new AMap.StationSearch());});this.getRoute(); // 獲取線路規(guī)劃this.getLocation(); // 獲取我的位置this.getBusStation(); // 站點(diǎn)查詢},methods: {// 1.路線規(guī)劃,不乘坐地鐵2.自行車出行(暫時(shí)不做)3.最快捷模式(綜合出行[包含地鐵]),用1,2,3來(lái)區(qū)分出行方式(goMethod)// 路線規(guī)劃文檔地址https://lbs.amap.com/api/javascript-api/reference/route-search#m_TransferPolicygetRoute(params) {params = 1;if (params === 1) {// map:AMap.Map對(duì)象, 展現(xiàn)結(jié)果的地圖實(shí)例 panel:結(jié)果列表的HTML容器id或容器元素 nightflag:是否計(jì)算夜班車 policy:公交換乘策略this.transOptions = {map: this.map,city: "西安",panel: "routeInfo",nightflag: true,policy: AMap.TransferPolicy.NO_SUBWAY,};} else if (params === 2) {this.transOptions = {map: this.map,city: "西安",panel: "routeInfo",nightflag: true,policy: AMap.TransferPolicy.NO_SUBWAY,};} else if (params === 3) {this.transOptions = {map: this.map,city: "西安",panel: "routeInfo",nightflag: true,policy: AMap.TransferPolicy.LEAST_TIME,};}// 構(gòu)造公交換乘類var transfer = new AMap.Transfer(this.transOptions);// 一:// 根據(jù)起、終點(diǎn)坐標(biāo)查詢公交換乘路線,使用父組件傳入的起終點(diǎn)經(jīng)緯度// transfer.search(new AMap.LngLat(108.9342500000, 34.2305300000), new AMap.LngLat(108.9470386505, 34.2593889736), function (status, result) {// // result即是對(duì)應(yīng)的公交路線數(shù)據(jù)信息// if (status === 'complete') {// // 出行計(jì)劃按照時(shí)間順序排序// const route = result.plans.sort(function (a, b) { return a.time - b.time })// this.routeListData = route// console.log(this.routeListData)// // console.log(this.routeListData)// } else {// console.log('公交路線數(shù)據(jù)查詢失敗' + result)// }// })// 二:// 根據(jù)起、終點(diǎn)坐標(biāo)查詢公交換乘路線,使用父組件傳入的起終點(diǎn)經(jīng)緯度transfer.search(new AMap.LngLat(108.93425, 34.23053),new AMap.LngLat(108.9470386505, 34.2593889736));AMap.event.addListener(transfer, "complete", (res) => {// res即是對(duì)應(yīng)的公交路線數(shù)據(jù)信息// res為獲取到的當(dāng)前位置的信息console.log(res);// 出行計(jì)劃按照時(shí)間順序排序const route = res.plans.sort(function (a, b) {return a.time - b.time;});this.routeListData = route;console.log(this.routeListData);}); // 返回出行方式信息AMap.event.addListener(transfer, "error", (err) => {console.log(err);}); // 返回出行方式信息出錯(cuò)信息},// 獲取當(dāng)前位置getLocation() {// 定義定位獲取當(dāng)前位置var geolocation = new AMap.Geolocation({enableHighAccuracy: true, // 是否使用高精度定位,默認(rèn):truetimeout: 100000, // 超過(guò)100秒后停止定位,默認(rèn):無(wú)窮大maximumAge: 0, // 定位結(jié)果緩存0毫秒,默認(rèn):0convert: true, // 自動(dòng)偏移坐標(biāo),偏移后的坐標(biāo)為高德坐標(biāo),默認(rèn):trueshowButton: false, // 顯示定位按鈕,默認(rèn):true// buttonPosition: 'LB', //定位按鈕??课恢?#xff0c;默認(rèn):'LB',左下角buttonOffset: new AMap.Pixel(10, 20), // 定位按鈕與設(shè)置的??课恢玫钠屏?#xff0c;默認(rèn):Pixel(10, 20)showMarker: false, // 定位成功后在定位到的位置顯示點(diǎn)標(biāo)記,默認(rèn):trueshowCircle: false, // 定位成功后用圓圈表示定位精度范圍,默認(rèn):truepanToLocation: true, // 定位成功后將定位到的位置作為地圖中心點(diǎn),默認(rèn):truezoomToAccuracy: true, // 定位成功后調(diào)整地圖視野范圍使定位位置及精度范圍視野內(nèi)可見,默認(rèn):false});// 一定要add添加// this.map.addControl(geolocation)geolocation.getCurrentPosition(); // 獲取用戶當(dāng)前的精確位置信息當(dāng)回調(diào)函數(shù)中的status為complete的時(shí)候表示定位成功AMap.event.addListener(geolocation, "complete", (res) => {// res為獲取到的當(dāng)前位置的信息console.log(res);}); // 返回定位信息AMap.event.addListener(geolocation, "error", (err) => {console.log(err);}); // 返回定位出錯(cuò)信息},// 獲取站點(diǎn)信息列表,使用父組件傳入輸入框的值getBusStation() {this.stationSearch = {pageIndex: 1, // 頁(yè)碼pageSize: 30, // 單頁(yè)顯示結(jié)果條數(shù)city: "029", // 確定搜索城市};var stationList = new AMap.StationSearch(this.stationSearch);// 一:// stationList.search('小', function (status, result) {// // result即是對(duì)應(yīng)的公交站點(diǎn)數(shù)據(jù)信息// if (status === 'complete' && result.info === 'OK') {// console.log(result)// console.log(this.stationListData)// } else {// console.log('公交路線數(shù)據(jù)查詢失敗' + result)// }// })// 二:stationList.search("西安鐘樓"); // mock一個(gè)假數(shù)據(jù)AMap.event.addListener(stationList, "complete", (res) => {// res為獲取到的當(dāng)前位置的信息this.stationListData = res;console.log(this.stationListData);}); // 返回定位信息AMap.event.addListener(stationList, "error", (err) => {console.log(err);}); // 返回定位出錯(cuò)信息},}, }; </script> <style scoped> .page {height: calc(100vh - 50px); } .map-content {position: relative; } #routeInfo {position: absolute;top: 0;right: 0; } </style>

    總結(jié)

    以上是生活随笔為你收集整理的vue 引入高德地图 路线规划的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。