<template>
<!-- 地图弹窗 含地图搜索 地图默认中心点为用户所属当地市政府 地图点击获取经纬度和 地址 -->
<el-dialog
title="选择地点"
:before-close="cancelbtn"
:visible="dialogHavePositon"
:width="setDialogWidth"
append-to-body
>
<div id="outer-box">
<div
v-show="dialogHavePositon"
id="container"
class="map"
tabindex="0"
></div>
<div id="panel" class="scrollbar1">
<div id="searchBar">
<input
id="searchInput"
autocomplete="off"
@input="inputchange($event)"
placeholder="输入关键字"
/>
</div>
<div id="searchResults"></div>
</div>
<!-- 地址栏 -->
</div>
<div class="input-item">
<div class="input-item-prepend">
<span class="input-item-text">地 址</span>
</div>
<input id="address" type="text" disabled />
</div>
<div slot="footer" class="dialog-footer">
<el-button @click="cancelbtn">取 消</el-button>
<el-button type="primary" @click="submitbtn">确 定</el-button>
</div>
</el-dialog>
</template>
<script>
// import AMap from "AMap";// 引入高德地图
import remoteLoad from "../../router/remoteLoad.js";
export default {
name: "dialogmap",
props: ["dialogHavePositon"],
data() {
return {
dialogWidth: "", //弹窗宽度,
poiinfo: "", //获取到poi的信息
keyword: "", //搜索框关键字
addressval: "", //地址框 显示地址用
};
},
mounted() {
this.$nextTick(() => {
this.init();
});
},
updated() {},
computed: {
setDialogWidth() {
var val = document.body.clientWidth;
const def = 1200; // 默认宽度
return def + "px";
// if (val < def) { //为移动端做准备的 最后确认过需求后此版本前台项目不需要兼容移动端 故暂废弃
// console.log('[ "<" ]', "<");
// return "100%";
// } else {
// console.log('[ ">" ]', ">");
// return def + "px";
// }
},
},
methods: {
inputchange(e) {
//当搜索框改变的时候 先清空地址栏
document.getElementById("address").value = "";
},
submitbtn() {
//确认按钮
let that = this;
that.$emit("poifn", that.poiinfo);
},
cancelbtn() {
//取消按钮
let that = this;
that.$emit("dialogshowfn");
},
async init() {
//地图初始化 及相关事件
let that = this;
// if (window.AMap && window.AMapUI) {
this.$nextTick(() => {
let { longitude, latitude } = that.$store.state.kqLayer;
let map = new AMap.Map("container", {
//初始化
center: [longitude, latitude], //中心点
resizeEnable: true, //可以调整大小
zoom: 14, //层级
});
AMapUI.loadUI(["misc/PoiPicker"], function (PoiPicker) {
//搜索及列表和结果点标记
var poiPicker = new PoiPicker({
input: "searchInput",
placeSearchOptions: {
map: map,
pageSize: 5,
},
searchResultsContainer: "searchResults",
});
poiPicker.on("poiPicked", function (poiResult) {
poiPicker.hideSearchResults();
var source = poiResult.source,
poi = poiResult.item;
if (source !== "search") {
map.clearMap(); // 清除地图上所有添加的覆盖物
//suggest来源的,同样调用搜索
poiPicker.searchByKeyword(poi.name);
} else {
//选中左侧菜单或者mark点
let opiinfo = new Object();
opiinfo.lnglat = poi.location;
opiinfo.gettype = "1";
opiinfo.opiinfo = poi;
opiinfo.address = poi.cityname + poi.adname + poi.address;
that.poiinfo = opiinfo;
document.getElementById("address").value =
poi.cityname + poi.adname + poi.address; //选中的地址给地址框 用任何vue的方式都会导致地图闪动故用此法
}
});
poiPicker.onCityReady(function () {
poiPicker.searchByKeyword("");
});
});
var marker = new AMap.Marker();
map.on("click", function (e) {
//监听地图点击事件
let lnglat = e.lnglat;
AMap.plugin("AMap.Geocoder", function () {
var geocoder = new AMap.Geocoder({
// city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
// city: "010",
});
geocoder.getAddress(lnglat, function (status, result) {
if (status === "complete" && result.info === "OK") {
map.clearMap(); // 清除地图上所有添加的覆盖物
let opiinfo = new Object();
opiinfo.lnglat = lnglat;
opiinfo.gettype = "0";
opiinfo.opiinfo = result.regeocode;
opiinfo.address = result.regeocode.formattedAddress;
that.poiinfo = opiinfo;
document.getElementById("address").value =
result.regeocode.formattedAddress; //选中的地址给地址框 用任何vue的方式都会导致地图闪动故用此法
document.getElementById("searchInput").value = ""; //清空搜索框
document.getElementById("searchResults").innerHTML = ""; //清空搜索
map.add(marker); //添加点标记
marker.setPosition(lnglat); //给点标记定位
}
});
});
});
});
// }
},
},
};
</script>
<style scoped lang="stylus" >
.input-item {
position: absolute;
bottom: 110px;
left: 480px;
vertical-align: top;
// border-radius .2rem
overflow: hidden;
>#address { // 地址框
display: inline-block;
height: 30px;
width: 20rem;
vertical-align: top;
padding: 0.375rem 0.75rem;
border-radius: 0 0.25rem 0.25rem 0;
border: 1px solid #ced4da;
color: #495057;
background: #fff;
box-sizing: border-box;
}
.input-item-prepend {
display: inline-block;
border-radius: 0.25rem 0 0 0.25rem;
height: 30px;
vertical-align: top;
background: #e9ecef;
color: #495057;
line-height: 30px;
text-align: center;
box-sizing: border-box;
border: 1px solid #ced4da;
.input-item-text {
font-size: 0.9rem;
width: 5rem;
padding: 0 1rem;
// display: inline-block;
// border:0;
text-align-last: justify;
font-weight: 400;
color: #495057;
background-color: #e9ecef;
border-radius: 0.25rem;
border-bottom-right-radius: 0;
border-top-right-radius: 0;
box-sizing: border-box;
}
}
}
.amap-sug-result {
z-index: 9999;
}
#outer-box {
height: 100%;
padding-right: 300px;
}
#container {
height: 500px;
width: 860px;
}
#panel {
position: absolute;
top: 85px;
bottom: 100px;
right: 20px;
max-height: 90%;
overflow-y: auto;
width: 300px;
z-index: 999;
border-left: 1px solid #eaeaea;
background-color: white;
}
#searchBar {
height: 30px;
background: #ccc;
font-size: 0px;
}
.sugg-item {
font-size: 20px;
}
#searchInput {
width: 100%;
height: 30px;
line-height: 30%;
-webkit-box-sizing: border-box;
box-sizing: border-box;
border: none;
border: 1px solid #ccc;
padding: 0 5px;
}
#searchResults {
overflow: auto;
height: calc(100% - 30px);
}
.amap_lib_placeSearch, .amap-ui-poi-picker-sugg-container {
border: none !important;
}
.amap_lib_placeSearch .poibox.highlight {
background-color: #cae1ff;
}
.poi-more {
display: none !important;
}
>>> .amap-ui-poi-picker-sugg-list {
font-size: 10px !important;
}
</style>