高德地图增加地理围栏

先上效果图片:

第一步引入高德地图:

在目录public -> index.html中引用该地址

 <script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=自己的key"></script>

第二步:

 如果需要使用搜索功能会报infocode:"10008" INVALID_USER_SCODE这个错误。

在目录public -> index.html中引用该地址之前加入,要不然没效果

<script type="text/javascript">
            window._AMapSecurityConfig = {
                securityJsCode:'安全秘钥',
            }
    </script>

第三步vue子模块:

<template>
  <div>
    <div class="periphery-search-box">
      <div class="search-box" id="myPageTop">
        <input id="tipinput" placeholder="请求输入关键字" v-model="keywords" @keydown.enter="addSearchFun" maxlength="12" />
        <div class="search-list-box" v-for="item in cearchList" v-if="openSearch">
          <div class="search-list-select" @click="selectSearch(item.location.lng,item.location.lat)">{{item.name}}</div>
        </div>
      </div>
    </div>

    <div id="container"></div>
    <div class="button-type">
      <button @click="openFun">编辑</button><button @click="closeFun">关闭</button>
      <button @click="addRailFun">增加</button>
    </div>
  </div>
</template>

<script>
  export default {
    name: "railMap",
    data() {
      return {
        map: null,
        icon: null,
        markerValue: 0,
        markerList: [],
        polyEditor: null,
        polygon: null,
        defaultCenter:this.center,
        defaultNodes:this.nodes,
        placeSearch: null,
        keywords: "",
        cearchList:[],
        openSearch:false
      }
    },
    mounted() {
      this.init();
    },
    methods: {
      init() {
        let then = this;
        this.defaultCenter = this.getDefaultCenter();
        this.map = new AMap.Map("container", {
          resizeEnable: true,
          center: this.defaultCenter,
          zoom: 13,
          viewMode: '3D',
        });
        AMap.plugin('AMap.PlaceSearch', function() {
          var autoOptions = {
            city: '全国'
          }
          then.placeSearch = new AMap.PlaceSearch(autoOptions);
        })
        // this.createIcon();
        // this.map.on("rightclick", this.closeFun)
        //this.map.on("rightclick", this.rightclick)
        this.addPolygon()
      },
      getDefaultCenter(){
        let dc = this.defaultCenter;
        if(dc == null || dc == undefined || dc.length <= 0){
          return [116.397428, 39.90923];
        }
        return dc;
      },
      //增加搜索
      addSearchFun() {
        let then = this;
        if (!this.keywords) {
          this.$message.error('搜索内容不能为空');
          return;
        }
        this.placeSearch.search(this.keywords, function(status, result) {
          if(status == "error")return;
          then.openSearch = true;
          then.cearchList = result.poiList.pois
        })
      },
      selectSearch(lng,lat){
        this.map.setCenter([lng,lat]);
        this.openSearch = false;
      },
      //增加多边形编辑
      addPolygon() {
        this.polygon = new AMap.Polygon({
          // path: path,
          // strokeColor: "#FF33FF",
          strokeWeight: 6,
          strokeOpacity: 0.2,
          fillOpacity: 0.4,
          fillColor: '#1791fc',
          zIndex: 50,
          bubble: true,
        })
        this.map.add([this.polygon])
        // 缩放地图到合适的视野级别
        this.map.setFitView()

        let then = this;
        //构造折线编辑对象,并开启折线的编辑状态
        this.map.plugin(["AMap.PolygonEditor"], function() {
          then.polyEditor = new AMap.PolygonEditor(then.map, then.polygon);
          // then.polyEditor.on("end", then.end)
          then.polyEditor.on("addnode", then.addnodeFun)
          then.polyEditor.on("adjust", then.adjustFun)
          then.polyEditor.on("removenode", then.removenodeFun)
        });
      },
      setPolygonNode(){
        let dn = this.defaultNodes;
        if(dn != null && dn != undefined && dn.length > 0){
          this.polygon.setPath(dn)
        }
      },
      closeFun() {
        this.polyEditor.close();
      },
      openFun() {
        this.polyEditor.open();
      },
      //结束
      end(v) {},
      //增加
      addnodeFun(t) {
        let s = this.judgeNode();
        if (!s) return;
        this.judgeArea();
      },
      //编辑
      adjustFun(t) {
        let s = this.judgeNode();
        if (!s) return;
        this.judgeArea();
      },
      //移除
      removenodeFun(t) {
        let s = this.judgeNode();
        if (!s) return;
        this.judgeArea();
      },
      judgeNode() {
        let nodeNumber = 100;
        let p = this.polygon.getPath();
        if (p.length > nodeNumber) {
          this.$message.error('节点不能大于' + nodeNumber + '个');
          return false;
        }
        return true;
      },
      judgeArea() {
        let areaSize = 100; //平分公里
        let area = this.polygon.getArea() / 100000000; //单位平方米,换算平分公里
        console.log(area)
        if (area > areaSize) {
          this.$message.error('选择面积不能大于' + areaSize + '平分公里');
          return false;
        }
        return true;
      },
      //增加初始化的围栏
      addRailFun() {
        if (!this.openConfirm("是否重新添加围栏,添加会覆盖原来的围栏")) return;
        this.closeFun();
        let c = this.map.getCenter();
        let lng = c.lng;
        let lat = c.lat;
        var newPath = []
        for (let i = 0; i < 4; i++) {
          let latlng = [];
          let p = [];

          let nlng = 0;
          let nlat = 0;
          if (i == 0) {
            nlng = lng;
            nlat = lat;
          } else if (i == 1) {
            nlng = lng + 0.01;
            nlat = lat;
          } else if (i == 2) {
            nlng = lng + 0.01;
            nlat = lat + 0.01;
          } else if (i == 3) {
            nlng = lng;
            nlat = lat + 0.01;
          }
          latlng.push(nlng);
          latlng.push(nlat);
          newPath.push(latlng);
        }
        this.polygon.setPath(newPath)
      },
      openConfirm(content) {
        return confirm(content);
      },
      //获取所有节点
      getNodes(){
        return this.polygon.getPath();
      },




      //下面是增加水滴标记
      createIcon() {
        // 创建 AMap.Icon 实例:
        this.icon = new AMap.Icon({
          size: new AMap.Size(40, 50), // 图标尺寸
          image: 'https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png', // Icon的图像
          imageOffset: new AMap.Pixel(0, 0), // 图像相对展示区域的偏移量,适于雪碧图等
          imageSize: new AMap.Size(30, 40) // 根据所设置的大小拉伸或压缩图片
        });
      },
      addMarker(label, lng, lat) {
        // 将 Icon 实例添加到 marker 上:
        var marker = new AMap.Marker({
          position: new AMap.LngLat(lng, lat),
          offset: new AMap.Pixel(-13, -30),
          icon: this.icon, // 添加 Icon 实例
          title: '北京',
          zoom: 13,
          // 设置是否可以拖拽
          draggable: true,
          cursor: 'move',
          label: label
        });
        marker.on("dragging", this.draggingFun);
        marker.on("dragend", this.dragendFun);
        this.addMarkerArrays(label, lng, lat);
        marker.setMap(this.map);
      },
      rightclick(e) {
        if (this.markerList.length >= 100) {
          alert("标记不能大于100个")
          return;
        }
        let ll = e.lnglat;
        this.addMarker("1", ll.lng, ll.lat);
      },
      draggingFun(e) {
        console.log(e)
      },
      dragendFun(e) {
        console.log(e, '结束')
      },
      //增加标记数组
      addMarkerArrays(label, lng, lat) {
        let t = {};
        t.label = label
        t.lng = lng
        t.lat = lat
        this.markerList.push(t);
      }
    },
    props:{
      center:{
        type:Array,
        default:null
      },
      nodes:{
        type:Array,
        default:null
      }
    }
  }
</script>

<style scoped lang="scss">
  #container {
    height: 600px;
  }

  .periphery-search-box {
    height:4px;

    .search-box {
      height: 160px;
      position: absolute;
      top: 60px;
      z-index: 88888888;
    }
    .search-list-box{
      background-color: #fff;
      .search-list-select{
        line-height: 30px;
        cursor: pointer;
      }
      .search-list-select:hover{
        background-color: #ccc;
      }
    }
  }

  .button-type {
    text-align: right;
    padding: 5px;
  }
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在高德地图中,可以通过设置围栏的 Dwell 功能来实现停留时间监听。具体实现步骤如下: 1. 创建地理围栏:使用高德地图提供的 API,调用相应的接口创建地理围栏。在创建地理围栏时,需要指定围栏的中心点、半径、形状等信息。 2. 设置围栏 Dwell 监听器:在创建地理围栏后,需要为其设置 Dwell 监听器。Dwell 监听器是一种特殊的围栏监听器,它可以监听设备在围栏内停留的时间。 3. 处理 Dwell 事件:当设备停留在围栏内的时间超过设定的阈值时,Dwell 监听器会触发相应的事件。可以在事件处理程序中进行相应的处理,比如发送通知、进行数据记录等。 下面是一个示例代码,用于创建一个带有 Dwell 监听器的地理围栏: ```java // 创建围栏中心点 LatLng center = new LatLng(39.123456, 116.234567); // 创建围栏 DPoint dCenter = new DPoint(center.latitude, center.longitude); float radius = 1000; // 半径为1000米 GeoFence fence = new GeoFence.Builder() .setCenter(dCenter) .setRadius(radius) .setDwellDelay(10 * 60 * 1000) // 设置停留时间阈值为10分钟 .build(); // 创建 Dwell 监听器 DwellGeoFenceListener listener = new DwellGeoFenceListener() { @Override public void onDwellStart(String fenceId) { // 设备进入围栏并开始停留 Log.i(TAG, "Device start dwell in fence: " + fenceId); } @Override public void onDwellEnd(String fenceId) { // 设备离开围栏或停留时间不足阈值 Log.i(TAG, "Device end dwell in fence: " + fenceId); } @Override public void onDwellTrigger(String fenceId, int duration) { // 设备停留时间超过阈值 Log.i(TAG, "Device dwell trigger in fence: " + fenceId + ", duration: " + duration); } }; // 添加围栏监听器和 Dwell 监听器 GeoFenceClient client = new GeoFenceClient(getApplicationContext()); client.addGeoFence(fence, listener); ``` 在上面的示例代码中,`setDwellDelay` 方法用于设置停留时间阈值,单位为毫秒。`DwellGeoFenceListener` 是一个特殊的围栏监听器,用于监听围栏内的停留事件。`onDwellStart` 方法用于处理设备进入围栏并开始停留的事件,`onDwellEnd` 方法用于处理设备离开围栏或停留时间不足阈值的事件,`onDwellTrigger` 方法用于处理设备停留时间超过阈值的事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值