addPoi.vue 14.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
<template>
  <v-full-layout ref="vFullLayout">
    <template slot="main">
      <div
        ref="map"
        id="map"
        class="map-container"
        v-loading="mapLoading"
      ></div>

      <!-- 地图工具 开始 -->
      <fence-map-controll
        @onRefreshMap="refreshMap"
        :isModalMap="true"
      ></fence-map-controll>
      <!-- 地图工具 end -->

      <!-- 地图搜索工具 开始 -->
      <div class="map-seacrh-wrap">
        <el-input
          placeholder="请输入搜索地址"
          id="poiMapSearch"
          size="small"
          maxlength="40"
          v-model="poiMapSearch"
          :disabled="readonlyStatus"
        >
          <template slot="prepend">搜索</template>
        </el-input>
      </div>
      <!-- 地图搜索工具 end -->

      <!-- 底部 开始 -->
      <div class="bottom_list_hide" v-show="hideFooter" @click="onHide">
        <i class="el-icon-arrow-up"></i>
        <span>显示</span>
      </div>
      <div class="bottom_list" v-show="!hideFooter">
        <div class="head">
          <div class="left">
            <span>POI基础信息</span>
          </div>
          <div class="right">
            <a href="javascript:void(0)" class="link" @click="onHide">收起</a>
          </div>
        </div>

        <div class="bottom_content">
          <el-form
            ref="poiForm"
            :model="form"
            :rules="formRules"
            label-width="100px"
            class="addPoiForm"
          >
            <div class="form_list">
              <el-form-item label="POI名称:" prop="poiName">
                <el-input v-model="form.poiName" maxlength="20"></el-input>
              </el-form-item>
              <el-form-item label="POI类型:" prop="poiType">
                <el-select v-model="form.poiType" class="small_select">
                  <el-option label="黑区圆心" :value="1"></el-option>
                  <el-option label="二押点" :value="2"></el-option>
                  <el-option label="异常聚集点" :value="3"></el-option>
                </el-select>
              </el-form-item>
              <div class="lnglat_box">
                <el-form-item label="经度:" prop="lng">
                  <el-input v-model="form.lng" disabled />
                </el-form-item>
                <el-form-item label="维度:" prop="lat">
                  <el-input v-model="form.lat" disabled />
                </el-form-item>
              </div>
            </div>
            <div class="form_list">
              <el-form-item label="描述:">
                <el-input
                  type="textarea"
                  v-model="form.remark"
                  rows="5"
                  resize="none"
                  maxlength="200"
                  show-word-limit
                ></el-input>
              </el-form-item>
            </div>
          </el-form>
          <div class="btn_box">
            <el-button type="primary" :loading="aloading" @click="saveInfo"
              >保存</el-button
            >
            <el-button type="default" @click="goListPage">取消</el-button>
          </div>
        </div>
      </div>
      <!-- 底部 end -->
    </template>
  </v-full-layout>
</template>
<script>
import mapService from "@/utils/mapService.js";
import "@/utils/mapEvent.js";
import { STATICIMAGE } from "@/constant";
import FenceMapControll from "./FenceMapControll";
import EventBus from "@/event-bus";
import { showToast, deepClone, showModal } from "@/utils/utils.js";
import {
  ADD_POI_INFO,
  GET_POI_TRACK,
  DELETE_POI_INFO,
  UPDATE_POI_INFO,
  CHECK_POI_NAME,
  GET_POI_INFO,
} from "@/api/services/poi";
export default {
  components: {
    FenceMapControll,
  },
  data() {
    return {
      map: null,
      mapLoading: true,
      form: {},
      formRules: {
        poiName: [
          { required: true, message: "请输入POI名称", trigger: "blur" },
          { validator: this.checkName, trigger: "blur" },
        ],
        poiType: [
          { required: true, message: "请选择POI类型!", trigger: "change" },
        ],
        lng: [
          {
            required: true,
            message: "经纬度不能为空,请在地图中标注!",
            trigger: "blur",
          },
        ],
        lat: [
          {
            required: true,
            message: "经纬度不能为空,请在地图中标注!",
            trigger: "blur",
          },
        ],
      },
      hideFooter: false,
      poi: {},
      poiMapSearch: "",
      poiMarker: null, // 通过搜索、右键创建的点标记
      aloading: false,
      trackList: [], // 历史poi点
      poiInfoWindow: null, // 当前的弹框
      markerList: {}, // poi点标记列表
      showMapText: false, //是否地图展示道路、标题
    };
  },
  mounted() {
    console.log(STATICIMAGE);
    this.initMap();
    EventBus.$on("uiMap.poi.marker.delete", this.removePoi);
  },
  methods: {
    // 地图初始化
    initMap() {
      let _this = this;
      let plugins = ["AMap.LngLat", "AMap.MassMarks", "AMap.Geocoder"];
      _this.$mapInit({ plugins: plugins }).then((AMap) => {
        _this.mapLoading = false;
        window.AMap = AMap;
        _this.map = mapService.init("map", {
          zoom: 15,
          showIndoorMap: false,
          showLabel: false,
        });
        _this.bindSearch();
        _this.initMapEvent();
        _this.getPoiTrack();
      });
    },

    // 创建地图右键菜单
    initMapEvent() {
      let _this = this;
      let _map = this.map;
      let _position = "";
      let _contextMenu = new AMap.ContextMenu();
      //右键添加Marker标记
      _contextMenu.addItem("设置POI点", function () {
        _contextMenu.close();
        _this.addPoiMarker(_position);
      });
      //地图绑定鼠标右击事件——弹出右键菜单
      _map.on("rightclick", function (e) {
        _contextMenu.open(_map, e.lnglat);
        _position = e.lnglat;
      });
    },

    // 批量添加海量点
    addMassMarkers(arr) {
      let _this = this;
      let _query = _this.$route.query;
      let markers = [];
      let _icon = {
        url: STATICIMAGE.POI_MASS_ICON,
        size: new AMap.Size(16, 21),
        anchor: new AMap.Pixel(3, 3),
      };

      for (let i = 0, len = arr.length; i < len; i++) {
        let k = arr[i];

        if (k.id != _query.id) {
          if (k.lnglat) {
            let m = k.lnglat.split(",");
            k.lng = m[0];
            k.lat = m[1];
            k.lnglat = m;
            markers.push(k);
          }
        }
      }
      let mass = new AMap.MassMarks(markers, {
        opacity: 0.8,
        zIndex: 10,
        cursor: "pointer",
        style: _icon,
      });

      mass.on("click", function (e) {
        console.log(e);
        _this.markerEvent(e.data);
      });
      mass.setMap(_this.map);
    },

    // 地图右键添加poi点
    addPoiMarker(options, type, isOpen) {
      let _this = this;
      let _map = _this.map;
      let _InfoWindow = _this.poiInfoWindow;
      if (_this.poiMarker) {
        _map.remove(_this.poiMarker);
        _InfoWindow && _InfoWindow.close();
        _this.poiMarker = null;
      }

      let _icon = new AMap.Icon({
        image: STATICIMAGE.POI_MARKER,
      });
      if (options.id) {
        _icon = new AMap.Icon({
          image: STATICIMAGE.POI_MASS_ICON,
          imageSize: new AMap.Size(16, 21),
        });
      }

      let _marker = new AMap.Marker({
        icon: _icon,
        position: [options.lng, options.lat],
        extData: options,
      });

      let _markerInfo = _marker.getExtData();
      //_marker.infoWindow = mapService.createPoiMarkerInfowindow(options);
      _marker.on("click", function (e) {
        _this.markerEvent(_markerInfo);
      });
      _marker.setMap(_this.map);

      // 历史marker存储起来,以id为键值,方便后续删除用
      if (options.id) {
        _this.$set(_this.markerList, options.id, _marker);
      }

      if (!options.id) {
        _this.poiMarker = _marker;
        _this.$set(_this.form, "lng", options.lng);
        _this.$set(_this.form, "lat", options.lat);
        _this.markerEvent(_markerInfo);
      }

      if (type && type == "search") {
        _this.setCenter(options.lng, options.lat);
      }

      // 默认打开信息弹框
      if (isOpen) {
        _this.markerEvent(options);
        _this.setCenter(options.lng, options.lat);
      }
    },

    // marker点击事件
    markerEvent(e) {
      let _this = this;
      console.log(e);
      let _params = e;
      const geocoder = new AMap.Geocoder({
        extensions: "all",
      });
      _this.setPoiInfo(e);
      geocoder.getAddress(
        new AMap.LngLat(e.lng, e.lat),
        function (status, result) {
          console.log(status);
          console.log(result);
          let _regeocode = result.regeocode || {};
          let _pois = _regeocode.pois || [];
          _params.address = _regeocode.formattedAddress || "";
          _params.near = _pois[0] ? _pois[0].name : "";
          let infoWindow = mapService.createPoiMarkerInfowindow(_params);
          _this.poiInfoWindow = infoWindow;
          infoWindow.open(_this.map);
        }
      );
    },

    // 设置poi信息
    setPoiInfo(row) {
      let _this = this;
      _this.$set(_this.form, "lng", row.lng);
      _this.$set(_this.form, "lat", row.lat);
      _this.$set(_this.form, "remark", row.remark || "");
      _this.$set(_this.form, "poiName", row.poiName || "");
      _this.$set(_this.form, "poiType", row.poiType || "");
      _this.$set(_this.form, "id", row.id || "");
      _this.$set(_this.form, "enableStatus", row.enableStatus || "");
      setTimeout(function () {
        _this.$refs.poiForm.clearValidate();
      }, 100);
    },

    // 隐藏
    onHide() {
      this.hideFooter = !this.hideFooter;
    },

    // 绑定搜索
    bindSearch() {
      const _this = this;
      AMap.plugin("AMap.AutoComplete", function () {
        let autoComplete = new AMap.AutoComplete({
          input: "poiMapSearch",
        });

        autoComplete.on("select", function (e) {
          console.log(e);
          let _poi = e.poi || {};
          let _params = {};
          _params.lng = _poi.location.lng;
          _params.lat = _poi.location.lat;

          _this.addPoiMarker(_params, "search");
        });
      });
    },

    // 返回列表页面
    goListPage() {
      let path = `/trialRun#/poi/index`;
      history.pushState({}, path, path);
    },

    // 获取参数
    getParams() {
      let params = {};
      let m = deepClone(this.form);
      params.poiName = m.poiName;
      params.poiType = m.poiType;
      params.lnglat = `${m.lng},${m.lat}`;
      params.remark = m.remark;
      if (m.id) {
        params.id = m.id;
        params.enableStatus = m.enableStatus;
      }
      return params;
    },

    // 保存信息
    saveInfo() {
      let _this = this;
      _this.$refs.poiForm.validate((valid) => {
        if (!valid) {
          return false;
        }
        let _params = _this.getParams();
        let _callBack = ADD_POI_INFO;
        if (_params.id) {
          _callBack = UPDATE_POI_INFO;
        }
        _this.aloading = true;
        _callBack(_params).then((res) => {
          console.log(res);
          _this.aloading = false;
          if (res.code == 200) {
            showToast({
              message: "保存成功",
              type: "success",
            });
            _this.goListPage();
          }
        });
      });
    },

    // 删除poi点
    removePoi(row) {
      console.log(row);
      let _this = this;
      let _message = "确认删除该POI信息吗?";
      let _typeList = {
        1: "黑区圆心",
        2: "二押点",
        3: "异常聚集点",
      };
      if (row.poiName) {
        _message = `确认删除【${row.poiName}】的【${
          _typeList[row.poiType]
        }】POI信息吗?`;
      }
      showModal({
        message: _message,
        type: "warning",
        showCancelButton: true,
        success: function () {
          _this.removeRequest(row);
        },
      });
    },

    // 删除请求
    removeRequest(row) {
      let _this = this;
      let _map = this.map;
      let _params = {};
      let _InfoWindow = this.poiInfoWindow;
      // id不存在,临时的poi点,删除marker就行了
      if (!row.id) {
        _map.remove(_this.poiMarker);
        _InfoWindow && _InfoWindow.close();
        _this.form = {};
        setTimeout(function () {
          _this.$refs.poiForm.clearValidate();
        }, 100);
        return false;
      }
      _params.id = row.id;
      DELETE_POI_INFO(_params).then((res) => {
        if (res.code == 200) {
          let _marker = _this.markerList[row.id];
          _map.remove(_marker);
          _InfoWindow && _InfoWindow.close();
          showToast({
            message: "删除成功",
            type: "success",
          });
          _this.form = {};
          setTimeout(function () {
            _this.$refs.poiForm.clearValidate();
          }, 100);
        }
      });
    },

    // 获取历史poi点
    getPoiTrack() {
      let _this = this;
      let _query = _this.$route.query;
      GET_POI_TRACK().then((res) => {
        if (res.code == 200) {
          let _list = res.data || [];
          _this.addMassMarkers(_list);

          // 编辑页面
          if (_query.id) {
            _this.getInfo();
          }
          _this.trackList = res.data || [];
          _this.showRoadTitle();
        }
      });
    },

    // 获取poi详情接口
    getInfo() {
      let _this = this;
      let _query = _this.$route.query;
      let _params = {};
      _params.id = _query.id;
      GET_POI_INFO(_params).then((res) => {
        console.log(res);
        let _obj = res.data || {};
        if (_obj.lnglat) {
          let arr = _obj.lnglat.split(",");
          _obj.lng = arr[0];
          _obj.lat = arr[1];
        }
        console.log(_obj);
        _this.addPoiMarker(_obj, "", true);
      });
    },

    // 校验名称
    checkName(rule, value, callback) {
      let _params = {};
      _params.poiName = value;
      if (this.form.id) {
        _params.id = this.form.id;
      }
      CHECK_POI_NAME(_params).then((res) => {
        if (res.code == 200) {
          callback();
        } else {
          callback(new Error(res.message));
        }
      });
    },

    // 设置地图中心点
    setCenter(lng, lat) {
      let _this = this;
      let _map = _this.map;
      if (_map) {
        console.log(lng, lat);
        setTimeout(function () {
          _map.setCenter([lng, lat]);
        }, 1000);
      }
    },

    // 展示隐藏地图标题
    showRoadTitle() {
      let _road = new AMap.TileLayer.RoadNet();
      let _title = new AMap.TileLayer();
      this.map.add([_title]);
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../assets/scss/addPoi.scss";
</style>