修改webpack.base.config.js(必须配不然会报错)
externals: {
'AMap': 'AMap'
}
引入
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>首页</title>
<script src="https://webapi.amap.com/maps?v=1.3&key=63343cf54c48885db27a9f563ec0a46f"></script>
</head>
注意:必须放在head里 不然容易报错
使用
初始化地图
<!--
* @Description: In User Settings Edit
* @Author: your name
* @Date: 2019-08-13 10:17:52
* @LastEditTime: 2019-08-16 15:22:00
* @LastEditors: Please set LastEditors
-->
<template>
<div>
<div class="map" id="mapContainer"></div>
</div>
</template>
<script>
import AMap from "AMap";
var map, driving;
export default {
data() {
return {
};
},
beforeRouteEnter(to, from, next) {
next();
},
methods: {
initMap() {
let _this = this;
map = new AMap.Map("mapContainer", {
zoom: 10,
resizeEnable: true
});
// 使用插件
AMap.plugin(["AMap.Driving", "AMap.Geolocation"], function() {
_this.location();
if(_this.$route.query.hasOwnProperty('lng')){
map.clearMap();
_this.drivingLine(_this.$route.query.lng, _this.$route.query.lat)
}else{
_this.getMapIndex();
}
});
}
},
mounted() {
this.initMap();
}
};
</script>
<style lang="less">
// 修改地图组件
.amap-geolocation-con {
bottom: 55px !important;
}
.map {
width: 100%;
height: 100%;
}
</style>
使用插件
初始化是就把插件也初始化 这样在外面也可以使用这个插件 不必在回调函数内
// 使用插件
AMap.plugin(["AMap.Driving", "AMap.Geolocation"], function() {
_this.location();
if(_this.$route.query.hasOwnProperty('lng')){
map.clearMap();
_this.drivingLine(_this.$route.query.lng, _this.$route.query.lat)
}else{
_this.getMapIndex();
}
});
路线规划
map.clearMap(); //清除地图所有标记
let _this = this;
// 规划路线必须要清除前一次的 否则路线会重叠多个
if (driving) { //一定要判断要不然会报错
driving.clear();// 清除历史规划路线
}
driving = new AMap.Driving({
map: map
});
driving.search(map.getCenter(), [lng, lat], function(status, result) {
if (status === "complete") {
console.log("绘制驾车路线完成");
driving.searchOnAMAP({
origin:result.origin,
destination:result.destination
});
_this.showInfo = false;
} else {
console.log("获取驾车数据失败:" + result);
}
});
完整代码
有定位,标点,路线规划功能 其他要使用可以直接看原生高德地图文档
<!--
* @Description: In User Settings Edit
* @Author: your name
* @Date: 2019-08-13 10:17:52
* @LastEditTime: 2019-08-16 15:22:00
* @LastEditors: Please set LastEditors
-->
<template>
<div class="map">
<div class="map-search">
<input
type="search"
id="keywords"
placeholder="搜索联盟成员"
v-model="keyword_name"
v-on:keydown.13="search"
/>
</div>
<div class="map" id="mapContainer"></div>
<ul class="cate">
<li @click="tab_search('')">
<img src="/static/images/all.png" alt="">
</li>
<li v-for="(item, index) in cate_list" :key="index" @click="tab_search(item.cate_name)">
<img :src="item.cate_icon" alt="">
</li>
</ul>
<div v-if="showWindow" :class="showInfo?'search-info slideUp':'search-info slideDown'">
<div class="info">
<div class="desc">
<h1>
{{infoWindow.title}}
<i @click="toList">更多</i>
</h1>
<p>{{infoWindow.desc}}</p>
</div>
<div
class="address"
ref="address"
@click="drivingLine(infoWindow.lng, infoWindow.lat)"
>
<div class="position">
<img src="../../../assets/images/fitMapList/position1.png" alt />
<span>{{infoWindow.distance}}km</span>
</div>
</div>
<ul class="list">
<li>
<p>{{infoWindow.address}}</p>
<span></span>
<a :href="'tel://' + infoWindow.tel">
<img src="../../../assets/images/fitMapList/phone.png" alt />
</a>
</li>
</ul>
</div>
<div class="border" @click="openInfo"></div>
<div class="close" @click="closeInfo"></div>
</div>
</div>
</template>
<script>
import AMap from "AMap";
var map, driving;
export default {
data() {
return {
showWindow: false,
showInfo: false,
position: [],
markers: [],
infoWindow: {},
keyword_name: "",
cate_list: []
};
},
beforeRouteEnter(to, from, next) {
next();
},
methods: {
closeInfo() {
this.showInfo = false;
},
openInfo() {
this.showInfo = true;
},
toList() {
this.$router.push({path: '/mapmarklist'})
},
getMapIndex() {
let _this = this;
this.$get("/fmmap/map/index").then(res => {
this.setMarkers(res.data.anchor_list);
});
},
setMarkers(markers) {
let _this = this;
markers.forEach(function(marker, index) {
let center = map.getCenter();
console.log(center)
let dis = Math.round(center.distance([marker.lng, marker.lat]));
marker.distance = (dis / 1000).toFixed(1);
var marker = new AMap.Marker({
map: map,
icon: "/static/images/position.png",
position: [marker.lng, marker.lat],
info: marker
});
marker.on("click", function(e) {
_this.showInfo = true;
_this.showWindow = true;
_this.infoWindow = e.target.G.info;
});
});
map.setFitView();
},
initMap() {
let _this = this;
map = new AMap.Map("mapContainer", {
zoom: 10,
resizeEnable: true
});
AMap.plugin(["AMap.Driving", "AMap.Geolocation"], function() {
_this.location();
if(_this.$route.query.hasOwnProperty('lng')){
map.clearMap();
_this.drivingLine(_this.$route.query.lng, _this.$route.query.lat)
}else{
_this.getMapIndex();
}
});
},
location() {
// 定位
var geolocation = new AMap.Geolocation({
enableHighAccuracy: true, //是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位,默认:无穷大
maximumAge: 0, //定位结果缓存0毫秒,默认:0
convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
showButton: true, //显示定位按钮,默认:true
buttonPosition: "LB", //定位按钮停靠位置,默认:'LB',左下角
buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
panToLocation: false, //定位成功后将定位到的位置作为地图中心点,默认:true
zoomToAccuracy: false //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
});
map.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, "complete", function(result) {
console.log("定位成功");
}); //返回定位信息
AMap.event.addListener(geolocation, "error", function(error) {
console.log(JSON.stringify(error));
});
},
drivingLine(lng, lat) {
map.clearMap();
let _this = this;
if (driving) {
// 清除历史规划路线
driving.clear();
}
driving = new AMap.Driving({
map: map
});
driving.search(map.getCenter(), [lng, lat], function(status, result) {
if (status === "complete") {
console.log("绘制驾车路线完成");
driving.searchOnAMAP({
origin:result.origin,
destination:result.destination
});
_this.showInfo = false;
} else {
console.log("获取驾车数据失败:" + result);
}
});
},
tab_search(name) {
this.keyword_name = name;
this.search();
},
search() {
map.clearMap();
this.showWindow = false;
this.$get("/fmmap/map/anchor/search", {
lng: map.getCenter().lng,
lat: map.getCenter().lat,
keyword_name: this.keyword_name,
uniacid: 1
}).then(res => {
this.setMarkers(res.data.anchor_list);
});
},
getCate() {
this.$get('/fmmap/map/cate/list').then(res => {
if(res.data.length == 0) return;
res.data.map(item => {
item.cate_anchor_icon = this.axios.defaults.baseUrl + item.cate_anchor_icon;
})
this.cate_list = res.data;
})
}
},
mounted() {
this.initMap();
this.getCate();
}
};
</script>
<style lang="less">
// 修改地图组件
.amap-geolocation-con {
bottom: 55px !important;
}
.map {
width: 100%;
height: 100%;
.map-search {
position: fixed;
top: 15px;
width: 100%;
height: 45px;
text-align: center;
z-index: 9999;
input {
box-sizing: border-box;
width: 90%;
height: 45px;
border-radius: 8px;
border: 0;
outline: none;
margin: 0 auto;
font-size: 14px;
color: #333;
font-family: "PingFangSC-Regular";
background: url("../../../assets/images/fitMapList/search.png") no-repeat
10px center #fff;
background-size: 18px 18px;
padding: 5px 8px 5px 32px;
}
ul {
width: 45px;
box-sizing: border-box;
margin-top: 10px;
position: absolute;
right: 10px;
li {
width: 40px;
height: 40px;
// border: 1px solid #e4e4e4;
margin-bottom: 4px;
border-radius: 50%;
img {
width: 40px;
height: 40px;
}
}
}
}
.search-info {
width: 97%;
height: 50%;
background: #fff;
margin: 0 auto;
position: fixed;
bottom: -45%;
left: 50%;
transform: translateX(-50%);
z-index: 9999;
&.slideUp {
animation: slideUp 0.4s forwards;
.close {
display: block;
}
}
&.slideDown {
animation: slideDown 0.4s forwards;
}
.close {
position: absolute;
right: 12px;
top: 12px;
width: 17px;
height: 17px;
background: url("../../../assets/images/fitMapList/cloase.png") no-repeat
center center;
display: none;
}
.border {
width: 45px;
border: 4px solid #eceaea;
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
border-radius: 10px;
}
.info {
margin-top: 35px;
width: 100%;
overflow-y: scroll;
height: 90%;
.desc {
padding: 10px;
border-bottom: 1px solid #e4e4e4;
padding-bottom: 6px;
h1 {
font-size: 0.34rem;
color: #333;
font-weight: 500;
margin-bottom: 0.2rem;
position: relative;
width: 100%;
height: auto;
padding-right: 0.5rem;
i {
position: absolute;
top: 0.06rem;
right: 0;
font-size: 0.24rem;
cursor: pointer;
}
}
.grade {
font-size: 0;
margin: 5px 0;
i {
display: inline-block;
width: 12px;
height: 12px;
background: url("../../../assets/images/fitMapList/start2.png")
no-repeat center center;
background-size: 12px 12px;
margin-right: 2px;
&.active {
background: url("../../../assets/images/fitMapList/start1.png")
no-repeat center center;
background-size: 12px 12px;
}
}
}
p {
font-size: 0.26rem;
color: #333;
}
}
.address {
padding: 10px;
border-bottom: 1px solid #e4e4e4;
.position {
color: #000;
font-size: 15px;
width: 100%;
height: 45px;
border: 1px solid #e4e4e4;
text-align: center;
line-height: 45px;
margin: 12px auto;
img {
display: inline-block;
width: 20px;
height: 20px;
position: relative;
top: 28%;
}
}
.label {
font-size: 0;
span {
font-size: 12px;
color: #999;
padding: 4px 6px;
border: 1px solid #979797;
border-radius: 4px;
margin-right: 6px;
}
}
}
.list {
padding: 10px;
border-bottom: 1px solid #e4e4e4;
li {
line-height: 40px;
background: url("../../../assets/images/fitMapList/position.png")
no-repeat left center;
background-size: 11px 16px;
padding-left: 20px;
font-size: 14px;
display: flex;
align-items: center;
position: relative;
p {
width: 82.2%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
span {
width: 1px;
height: 22px;
background: #e4e4e4;
display: block;
}
a {
display: block;
width: 0.6rem;
height: 0.6rem;
display: flex;
position: absolute;
right: 0;
top: 0;
padding-top: 0.2rem;
img {
width: 0.4rem;
height: 0.4rem;
align-self: center;
}
}
}
}
}
}
}
// 图标样式
.cate{
position: fixed;
right: .4rem;
top: 1.5rem;
z-index: 9999;
li{
width: 0.8rem;
height: 0.8rem;
margin-top: .2rem;
border-radius: 50%;
img{
width: 100%;
}
}
}
@keyframes slideDown {
0% {
bottom: 0;
}
100% {
bottom: -45%;
}
}
@keyframes slideUp {
0% {
bottom: -45%;
}
100% {
bottom: 0;
}
}
</style>