生活随笔
收集整理的這篇文章主要介紹了
vue 引入高德地图 路线规划
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
由于vue-amap不支持路線規(guī)劃,因此不予采納。
效果如圖
在index.html的header中引入
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=e72a9f0cac3b081df05259299454cf1a"></script>
<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, 添加如下代碼,記得重啟項目!!!!
externals
: {AMap
: 'AMap',AMapUI
: 'AMapUI'
},
繪制地圖并規(guī)劃路線(關(guān)鍵代碼)
用來放地圖
<div class="page" id="map-container"></div>
用來放路線規(guī)劃
<div id="panel"></div>
初始化
this.map = new AMap.Map("map-container", {resizeEnable: true,center: [108.9470386505, 34.2593889736], // 地圖中心點zoom: 16, // 地圖顯示的縮放級別
});
公交路線查詢
new AMap.Transfer({map: this.map,panel: "panel",}).search([{ keyword: "寧波大學(xué)", city: "寧波" },{ keyword: "寧波老外灘", city: "寧波" },],function (status, data) {console.log(data);});
完整代碼(精簡版)
<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], zoom
: 16, });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(); },methods
: {getRoute() {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
>
完整代碼(定位、公交站點搜索、路線規(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
, },mounted() {this.map
= new AMap.Map("map-container", {resizeEnable
: true,center
: [108.9470386505, 34.2593889736], zoom
: 16, });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(); this.getLocation(); this.getBusStation(); },methods
: {getRoute(params
) {params
= 1;if (params
=== 1) {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,};}var transfer
= new AMap.Transfer(this.transOptions
);transfer
.search(new AMap.LngLat(108.93425, 34.23053),new AMap.LngLat(108.9470386505, 34.2593889736));AMap
.event
.addListener(transfer
, "complete", (res
) => {console
.log(res
);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
);}); },getLocation() {var geolocation
= new AMap.Geolocation({enableHighAccuracy
: true, timeout
: 100000, maximumAge
: 0, convert
: true, showButton
: false, buttonOffset
: new AMap.Pixel(10, 20), showMarker
: false, showCircle
: false, panToLocation
: true, zoomToAccuracy
: true, });geolocation
.getCurrentPosition(); AMap
.event
.addListener(geolocation
, "complete", (res
) => {console
.log(res
);}); AMap
.event
.addListener(geolocation
, "error", (err
) => {console
.log(err
);}); },getBusStation() {this.stationSearch
= {pageIndex
: 1, pageSize
: 30, city
: "029", };var stationList
= new AMap.StationSearch(this.stationSearch
);stationList
.search("西安鐘樓"); AMap
.event
.addListener(stationList
, "complete", (res
) => {this.stationListData
= res
;console
.log(this.stationListData
);}); AMap
.event
.addListener(stationList
, "error", (err
) => {console
.log(err
);}); },},
};
</script
>
<style scoped
>
.page
{height
: calc(100vh
- 50px
);
}
.map
-content
{position
: relative
;
}
#routeInfo
{position
: absolute
;top
: 0;right
: 0;
}
</style
>
總結(jié)
以上是生活随笔為你收集整理的vue 引入高德地图 路线规划的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。