Android地理围栏

1、请求地理围栏,并向围栏管理端添加

private void addFenceInAndStay() {
        mClientInAndStayAction = new GeoFenceClient(mContext);
        mClientInAndStayAction.createPendingIntent(GEOFENCE_BROADCAST_ACTION);
        mClientInAndStayAction.setGeoFenceListener(this);
        mClientInAndStayAction.setActivateAction(GeoFenceClient.GEOFENCE_IN | GeoFenceClient.GEOFENCE_STAYED);
        mClientInAndStayAction.addGeoFence("合肥", "快餐厅", "六安", 2, String.valueOf(mCustomID));
        mCustomID++;
        mClientInAndStayAction.addGeoFence("kfc", "快餐厅", new DPoint(39.982375,116.305292), 5000, 2, String.valueOf(mCustomID));
        mCustomID++;
        mClientInAndStayAction.addGeoFence("安庆", String.valueOf(mCustomID));
        mCustomID++;
    }

2、绘制地理围栏

1):绘制多边形 。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yPMhBleT-1663808467442)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/605a2cfe746f44bb80e353762919064e~tplv-k3u1fbpfcp-watermark.image?)]

private void drawPolygon(GeoFence fence) {
        final List<List<DPoint>> pointList = fence.getPointList();
        if (null == pointList || pointList.isEmpty()) {
            return;
        }
        List<Polygon> polygonList = new ArrayList<Polygon>();
        for (List<DPoint> subList : pointList) {
            if (subList == null) {
                continue;
            }
            List<LatLng> lst = new ArrayList<LatLng>();

            PolygonOptions polygonOption = new PolygonOptions();
            for (DPoint point : subList) {
                lst.add(new LatLng(point.getLatitude(), point.getLongitude()));
//                boundsBuilder.include(
//                        new LatLng(point.getLatitude(), point.getLongitude()));
            }
            polygonOption.addAll(lst);

            polygonOption.fillColor(mContext.getResources().getColor(R.color.fill));
            polygonOption.strokeColor(mContext.getResources().getColor(R.color.stroke));
            polygonOption.strokeWidth(4);
            Polygon polygon = mAMap.addPolygon(polygonOption);
            polygonList.add(polygon);
            mCustomEntitys.put(fence.getFenceId(), polygonList);
        }
    }

2)绘制圆形。

private void drawCircle(GeoFence fence) {
    CircleOptions option = new CircleOptions();
    option.fillColor(mContext.getResources().getColor(R.color.fill));
    option.strokeColor(mContext.getResources().getColor(R.color.stroke));
    option.strokeWidth(4);
    option.radius(fence.getRadius());
    DPoint dPoint = fence.getCenter();
    option.center(new LatLng(dPoint.getLatitude(), dPoint.getLongitude()));
    Circle circle = mAMap.addCircle(option);
    mCustomEntitys.put(fence.getFenceId(), circle);
}

3、向地图添加围栏

public void drawFenceToMap() {
        Iterator iter = fenceMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String) entry.getKey();
            GeoFence val = (GeoFence) entry.getValue();
            if (!mCustomEntitys.containsKey(key)) {
                Log.d("LG", "添加围栏:" + key);
                drawFence(val);
            }
        }
    }

4、地理围栏的可视化

// 当前的坐标点集合,主要用于进行地图的可视区域的缩放
private LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
LatLngBounds bounds = boundsBuilder.build();
mAMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 150));

5、切换地理围栏

1)、清除地理围栏:
aMap.clear(true),true表示保留定位点。

2)、显示地理围栏:
boundsBuilder.include(new LatLng(latitude, longitude));
draw(),也就是要显示的地理围栏

6、注意 Github代码

APP包名,签名信息,跟地图注册申请的key保持一致。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值