移动端地理围栏

示例展示如何在移动端运用地理围栏,以及如何结合地图进行地理围栏的图像展示。
下载源代码
00:00 / 00:12
体验移动端 扫码体验移动端

使用场景

可用于车辆的区域预警,人员区域监控,防止走失等场景。

用到产品

iOS 定位 SDK

核心类/接口

可参考:iOS 地理围栏开发指南

核心难点

请求围栏:

self.geoFenceManager.activeAction = AMapGeoFenceActiveActionInside | AMapGeoFenceActiveActionOutside | AMapGeoFenceActiveActionStayed; //监听进入、退出、停留事件,默认值为AMapGeoFenceActiveActionInside

[self.geoFenceManager removeAllGeoFenceRegions];  //移除所有已经添加的围栏,如果有正在请求的围栏也会丢弃

[self.geoFenceManager addKeywordPOIRegionForMonitoringWithKeyword:@"麦当劳" POIType:@"快餐厅" city:@"北京" size:2 customID:@"poi_keyword"]; //POI关键词

[self.geoFenceManager addAroundPOIRegionForMonitoringWithLocationPoint:coordinate aroundRadius:5000 keyword:@"肯德基" POIType:@"快餐厅" size:2 customID:@"poi_around"]; //POI周边

[self.geoFenceManager addDistrictRegionForMonitoringWithDistrictName:@"西城区" customID:@"district_1"];  //行政区域

//地理围栏状态改变时回调,当围栏状态的值发生改变,定位失败都会调用
- (void)amapGeoFenceManager:(AMapGeoFenceManager *)manager didGeoFencesStatusChangedForRegion:(AMapGeoFenceRegion *)region customID:(NSString *)customID error:(NSError *)error {
    if (error) {
        NSLog(@"status changed error %@",error);
    }else{
        NSLog(@"status changed %@",[region description]);
    }
}
self.geoFenceManager.activeAction = [AMapGeoFenceActiveAction.inside , AMapGeoFenceActiveAction.outside , AMapGeoFenceActiveAction.stayed ]//进入,离开,停留都要进行通知

self.geoFenceManager.removeAllGeoFenceRegions()  //移除所有已经添加的围栏,如果有正在请求的围栏也会丢弃

self.geoFenceManager.addKeywordPOIRegionForMonitoring(withKeyword: "北京大学", poiType: "高等院校", city: "北京", size: 20, customID: "poi_1")  //POI关键词

self.geoFenceManager.addAroundPOIRegionForMonitoring(withLocationPoint: coordinate, aroundRadius: 10000, keyword: "肯德基", poiType: "050301", size: 20, customID: "poi_2") //POI周边

self.geoFenceManager.addDistrictRegionForMonitoring(withDistrictName: "海淀区", customID: "district_1")   //行政区域

//地理围栏状态改变时回调,当围栏状态的值发生改变,定位失败都会调用
func amapGeoFenceManager(_ manager: AMapGeoFenceManager!, didGeoFencesStatusChangedFor region: AMapGeoFenceRegion!, customID: String!, error: Error!) {
    if error == nil {
        print("status changed \(region.description)")
    } else {
        print("status changed error \(error)")
    }
}

在地图上绘制围栏的方法如下,根据围栏的边界绘制圆形和多边形。

//添加地理围栏对应的Overlay,方便查看。地图上显示圆
- (MACircle *)showCircleInMap:(CLLocationCoordinate2D )coordinate radius:(NSInteger)radius {
    MACircle *circleOverlay = [MACircle circleWithCenterCoordinate:coordinate radius:radius];
    [self.mapView addOverlay:circleOverlay];
    return circleOverlay;
}

//地图上显示多边形
- (MAPolygon *)showPolygonInMap:(CLLocationCoordinate2D *)coordinates count:(NSInteger)count {
    MAPolygon *polygonOverlay = [MAPolygon polygonWithCoordinates:coordinates count:count];
    [self.mapView addOverlay:polygonOverlay];
    return polygonOverlay;
}
//添加地理围栏对应的Overlay,方便查看。地图上显示圆
    func showCircle(inMap coordinate: CLLocationCoordinate2D, radius: CLLocationDistance) -> MACircle {
        let circleOverlay = MACircle(center: coordinate, radius: radius)
        self.mapView.add(circleOverlay)
        return circleOverlay!
    }



下载源代码
00:00 / 00:12
体验移动端 扫码体验移动端

使用场景

可用于车辆的区域预警,人员区域监控,防止走失等场景。

用到产品

Android 定位 SDK

核心类/接口

可参考:Android地理围栏开发指南

核心难点

请求围栏:

//请求围栏,并向围栏管理端添加
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++;
    }

在地图上绘制围栏:

//绘制多边形 
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);
        }
    }
//绘制圆形
    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);
    }
//向地图添加围栏
    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);
            }
        }
    }
//根据围栏类型决定绘制圆形围栏还是多边形围栏
    private void drawFence(GeoFence fence) {
        switch (fence.getType()) {
            case GeoFence.TYPE_ROUND:
            case GeoFence.TYPE_AMAPPOI:
                drawCircle(fence);
                break;
            case GeoFence.TYPE_POLYGON:
            case GeoFence.TYPE_DISTRICT:
                drawPolygon(fence);
                break;
            default:
                break;
        }


返回顶部 示例中心 常见问题 智能客服 公众号
二维码