<template>
<div class="tmap">
<div class="btn">
<button @click="change2D">2D视图</button>
<button @click="change3D">3D视图</button>
<button @click="moveToLocal">平移</button>
<button @click="createInfoWindows">开启信息窗口</button>
<button @click="destroyedInfoWindows">关闭信息窗口</button>
<div>
<input id="tipinput" type="text" placeholder="请输入地名" v-model="keywords" />
<button @click="searchEvent">搜索</button>
</div>
</div>
<div class="map" id="amap-container" ref="tmap" style="width:100%;height:400px"></div>
</div>
</template>
<script>
export default {
name: "TMap",
data() {
return {
map: {},
center: {},
AMap: {},
marker: null,
keywords: "",
infoWindow: {},
placeSearch: {},
hotspotoverEvent: {},
auto: {}
};
},
props: {
aMapKey: {
default: "85c467ada7b08d4b909e233d37e2d7bb",
type: String
}
},
mounted() {
this.loadScript(() => {
this.initTMap();
});
},
methods: {
initTMap(
ViewMode = "3D",
resizeEnable = true,
rotateEnable = true,
pitchEnable = true,
pitch = 43.5
) {
this.AMap = window.AMap;
let defaultCenter = [102.66665434472793, 25.031639292195205];
this.map = new this.AMap.Map("amap-container", {
center: defaultCenter,
zoom: 17,
isHotspot: true,
viewMode: ViewMode,
buildingAnimation: true,
resizeEnable: resizeEnable,
rotateEnable: rotateEnable,
pitchEnable: pitchEnable,
pitch: pitch
});
this.loadedMap(() => {
this.createAMapPlugins();
this.createMarker();
this.map.on("click", e => {
let clickPoint = [e.lnglat.getLng(), e.lnglat.getLat()];
this.moveToLocal(clickPoint);
});
});
},
createMarker(point) {
this.marker = new this.AMap.Marker({
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
position: point
});
this.map.add(this.marker);
},
destroyedInfoWindows() {
this.AMap.event.removeListener(this.hotspotoverEvent);
this.infoWindow.close();
},
createInfoWindows() {
this.hotspotoverEvent = this.AMap.event.addListener(
this.map,
"hotspotover",
result => {
this.placeSearch.getDetails(result.id, (status, result) => {
if (status === "complete" && result.info === "OK") {
this.placeSearch_CallBack(result);
}
});
}
);
},
placeSearch_CallBack(data) {
window.console.log(data);
let poiArr = data.poiList.pois;
let location = poiArr[0].location;
this.infoWindow.setContent(this.createContent(poiArr[0]));
this.infoWindow.open(this.map, location);
},
createContent(poi) {
let s = [];
s.push(
'<div class="info-title">' +
poi.name +
'</div><div class="info-content">' +
"地址:" +
poi.address
);
s.push("电话:" + poi.tel);
s.push("类型:" + poi.type);
s.push("<div>");
return s.join("<br>");
},
loadedMap(cb) {
this.map.on("complete", () => {
cb && cb();
});
},
createAMapPlugins() {
this.AMap.plugin(
[
"AMap.ToolBar",
"AMap.PlaceSearch",
"AMap.AdvancedInfoWindow",
"AMap.Autocomplete"
],
() => {
let toolbar = new this.AMap.ToolBar();
this.map.addControl(toolbar);
this.placeSearch = new this.AMap.PlaceSearch();
this.infoWindow = new this.AMap.AdvancedInfoWindow({
retainWhenClose: true
});
}
);
},
moveToLocal(point) {
if (this.marker != null) {
this.map.remove(this.marker);
this.marker = null;
}
this.createMarker(point);
this.map.panTo(point);
},
searchEvent() {
this.auto = new this.AMap.Autocomplete({
input: "tipinput"
});
let keys = document.getElementById("tipinput").value;
this.keywords = keys;
this.auto.search(this.keywords, (status, result) => {
console.log(status, result);
this.moveToLocal([
result.tips[0].location.lng,
result.tips[0].location.lat
]);
});
},
change2D() {
this.map.destroy();
this.initTMap("2D");
},
change3D() {
this.map.destroy();
this.initTMap();
},
loadScript(callback) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = `https://webapi.amap.com/maps?v=1.4.15&key=${this.aMapKey}`;
document.body.appendChild(script);
if (script.readyState) {
script.onreadystatechange = function() {
if (
script.readyState == "complete" ||
script.readyState == "loaded"
) {
script.onreadystatechange = null;
callback && callback();
}
};
} else {
script.onload = function() {
callback && callback();
};
}
}
},
destroyed() {
this.map.destroy();
}
};
</script>
<style>
.info-title {
font-weight: bolder;
color: #fff;
font-size: 14px;
line-height: 26px;
padding: 0 0 0 6px;
background: #25a5f7;
}
.info-content {
padding: 4px;
color: #666666;
line-height: 23px;
font: 12px Helvetica, "Hiragino Sans GB", "Microsoft Yahei", "微软雅黑", Arial;
}
.info-content img {
float: left;
margin: 3px;
}
.amap-info-combo .keyword-input {
height: auto;
}
</style>
转载请注明原文地址:https://ipadbbs.8miu.com/read-57865.html