123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <template>
- <div>
- <el-autocomplete
- popper-class="my-autocomplete"
- v-model="addressName"
- :fetch-suggestions="querySearch"
- placeholder="请输入地址查找"
- @select="handleSelect"
- >
- <template slot-scope="{ item }">
- <div class="name">
- {{ item.name }} <span>({{ item.cityname + item.adname }})</span>
- </div>
- </template>
- </el-autocomplete>
- <div id="container"></div>
- </div>
- </template>
- <script>
- export default {
- props: ['params'],
- data() {
- return {
- map: '',
- placeSearch: '',
- marker: '',
- addressName: '', //地址查询名称
- poisArray: [],
- thisMapPot: {}
- };
- },
- methods: {
- init() {
- let _this = this;
- this.map = new AMap.Map('container', {
- resizeEnable: true, //是否监控地图容器尺寸变化
- zoom: 11 //初始化地图层级
- });
- this.map.on('complete', () => {
- if (!!_this.params.location) {
- let arr = this.params.location.split(',');
- this.addIcon({ lng: arr[0], lat: arr[1] });
- }
- });
- this.map.on('click', this.mapClick);
- this.placeSearch = new AMap.PlaceSearch({
- map: _this.map
- });
- AMap.event.addListener(this.placeSearch, 'markerClick', (e) => {
- this.thisMapPot = e.event.lnglat;
- this.$message.success('选择成功');
- });
- },
- mapClick(e) {
- this.addIcon(e.lnglat);
- this.$message.success('选择成功');
- },
- addIcon(e) {
- if (!!this.marker) {
- this.map.remove(this.marker);
- this.marker = '';
- }
- this.thisMapPot = e;
- let loc = [e.lng, e.lat];
- this.marker = new AMap.Marker({
- icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png',
- position: loc,
- map: this.map
- });
- this.marker.setLabel({
- offset: new AMap.Pixel(-50, 35),
- content: !!this.params.data.pointName ? `巡更名称:${this.params.data.pointName}` : `巡更经纬度:${e.lng},${e.lat}`
- });
- this.map.setFitView();
- },
- submit() {
- this.params.callback && this.params.callback(this.thisMapPot);
- this.$emit('close');
- },
- togglePlaceSearch(val, resolve) {
- this.placeSearch.search(val, (status, result) => {
- if (result.info == 'OK' && status == 'complete') {
- this.poisArray = result.poiList.pois;
- }
- resolve && resolve(true);
- });
- },
- handleSelect(item) {
- this.addressName = item.name;
- this.placeSearch.search(item.name);
- },
- querySearch(queryString, cb) {
- new Promise((resolve) => {
- this.togglePlaceSearch(queryString, resolve);
- }).then(() => {
- if (this.poisArray.length !== 0) {
- cb(this.poisArray);
- }
- });
- }
- },
- destroyed() {
- this.map && this.map.destroy();
- this.marker = '';
- },
- created() {
- this.$nextTick(() => {
- this.init();
- });
- }
- };
- </script>
- <style scoped lang="scss">
- /* --------------------------------高德地图样式----------------------------- */
- #container {
- margin-top: 20px;
- width: 100%;
- height: 400px;
- /deep/ .amap-marker-label {
- border: 0 none;
- background-color: #fff;
- white-space: nowrap;
- box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.3);
- border-radius: 5px;
- }
- // /deep/ .amap-marker-label:after {
- // position: absolute;
- // border: 5px solid transparent;
- // border-top-color: #fff;
- // top: 19px;
- // left: 43%;
- // content: '';
- // width: 0;
- // height: 0;
- // }
- }
- .my-autocomplete {
- li {
- line-height: normal;
- padding: 7px;
- .name {
- text-overflow: ellipsis;
- overflow: hidden;
- span {
- font-size: 12px;
- color: #b4b4b4;
- }
- }
- }
- }
- </style>
|