地理围栏
功能场景
本示例展示了如何根据自身业务需要,创建不同种类和模式的地理围栏,用于车辆区域提醒、智能活动推送等场景。
Android
iOS
//mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/地理围栏.mp4
1590746640|//mapopen-website-wiki.bj.bcebos.com/demos/newqrcodes/地理围栏.png
扫码体验
使用产品
Android定位SDK|/index.php?title=android-locsdk
>
地理围栏|/index.php?title=android-locsdk/guide/addition-func/loc-geofence
下载源码
//mapopen-website-wiki.bj.bcebos.com/demos/location/GeoFenceDemo.zip
核心接口
类
接口
描述
备注
GeoFenceClient
addGeoFence(final String keyword, String poiType,final String city, int size,String customId);
添加围栏(POI行政区划检索围栏)
定位SDK V8.2.0 之后支持
GeoFenceClient
setActivateAction(int action)
设置当前创建的围栏的触发行为
定位SDK V8.2.0 之后支持
GeoFenceClient
removeGeoFence()
清除所有围栏
定位SDK V8.2.0 之后支持
GeoFenceListener
onGeoFenceCreateFinished(List<GeoFence> geoFenceList,int errorCode)
监听围栏状态回调 geoFenceList :本次创建的围栏列表(创建成功才有,不成功为空)errorCode:错误码 customId:本次操作对应的自定义业务ID(如果添加的时候没有设置customId,回调中也不会有)
定位SDK V8.2.0 之后支持
BaiduMap
addOverlay(OverlayOptions options)
向地图添加一个 Overlay
地图SDK
核心代码
示例Demo展示的是(圆形围栏,多边形围栏,POI行政区划检索围栏,POI圆形区域检索围栏,行政区域围栏)多种添加围栏添加方式。
1.添加围栏
JAVA
/** * 添加围栏 */ private void addFence () { // 初始化地理围栏 fenceClient = new GeoFenceClient(getApplicationContext()); // 创建pendingIntent fenceClient.createPendingIntent(GEOFENCE_BROADCAST_ACTION); // 在即将触发侦听行为时允许开启高精度定位模式(开启gps定位,gps定位结果优先) fenceClient.isHighAccuracyLoc(true); fenceClient.setGeoFenceListener(this); // 设置地理围栏的触发行为,默认为进入 fenceClient.setActivateAction(GeoFenceClient.GEOFENCE_IN); //创建一个中心点坐标 DPoint centerPoint = new DPoint(40.051D,116.300D); // 添加圆形围栏 fenceClient.addGeoFence (centerPoint,GeoFenceClient.BD09LL,100,"业务ID"); // 添加行政区 fenceClient.addGeoFence("海淀区","业务ID"); // 添加POI行政区划检索围栏 fenceClient.addGeoFence("百度大厦","办公楼","北京",1," 0001"); // 添加POI圆形区域检索围栏 fenceClient.addGeoFence("一点点","餐饮",centerPoint,GeoFenceClient.BD09LL,1000F,10,"业务ID"); List<DPoint> points = new ArrayList<>(); DPoint pointa = new DPoint(40.051D,116.300D); DPoint pointb = new DPoint(40.051D,116.300D); DPoint pointc = new DPoint(40.051D,116.300D); points.add(pointa); points.add(pointb); points.add(pointc); // 添加多边形围栏 fenceClient.addGeoFence( points,GeoFenceClient.BD09LL, "业务ID"); }
复制
深色
复制成功
2.监听围栏状态回调
JAVA
List<GeoFence> fenceList = new ArrayList<>(); @Override public void onGeoFenceCreateFinished(final List<GeoFence> geoFenceList, int errorCode, String customId) { Message msg = Message.obtain(); if (errorCode == GeoFence.ADDGEOFENCE_SUCCESS) { fenceList.addAll(geoFenceList); msg.obj = customId; msg.what = 0; } else { msg.arg1 = errorCode; msg.what = 1; } handler.sendMessage(msg); }
复制
深色
复制成功
3.在地图上绘制围栏
JAVA
private void drawFence(GeoFence fence) { switch (fence.getType()) { case GeoFence.TYPE_ROUND: drawCircle(fence, false); break; case GeoFence.TYPE_BDMAPPOI: drawCircle(fence, true); break; case GeoFence.TYPE_POLYGON: drawPolygon(fence); break; default: break; } // 设置所有maker显示在当前可视区域地图中 LatLngBounds bounds = boundsBuilder.build(); MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngBounds(bounds, 50, 50, 50, 50); // 更新地图状态 mBdMap.animateMapStatus(mapStatusUpdate); removeMarkers(); } /** * 绘制圆 */ private void drawCircle(GeoFence fence, boolean isPoi) { LatLng center; int radius; if (isPoi) { BDLocation bdLocation = new BDLocation(); bdLocation.setLatitude(fence.getCenter().getLatitude()); bdLocation.setLongitude(fence.getCenter().getLongitude()); BDLocation tempLocation = LocationClient .getBDLocationInCoorType(bdLocation, BDLocation.BDLOCATION_GCJ02_TO_BD09LL); center = new LatLng(tempLocation.getLatitude(), tempLocation.getLongitude()); } else { center = centerLatLng; } radius = (int) fence.getRadius(); // 绘制一个圆形 if (center == null) { return; } mBdMap.addOverlay(new CircleOptions().center(center) .radius(radius) .fillColor(0x666495ED) // 填充颜色 .stroke(new Stroke(3, 0xE66495ED))); boundsBuilder.include(center); if (!isPoi) { centerLatLng = null; } } /** * 绘制Polygon */ private void drawPolygon(GeoFence fence) { final List<DPoint> pointList = fence.getPoints(); if (null == pointList || pointList.isEmpty()) { return; } List<LatLng> lst = new ArrayList<>(); for (DPoint point: pointList) { lst.add(new LatLng(point.getLatitude(), point.getLongitude())); boundsBuilder.include( new LatLng(point.getLatitude(), point.getLongitude())); } mBdMap.addOverlay(new PolygonOptions() .points(polygonPoints) .fillColor(0x666495ED) // 填充颜色 .stroke(new Stroke(5, 0xE66495ED))); if (polygonPoints!= null && polygonPoints.size() > 0) { polygonPoints.clear(); } }
复制
深色
复制成功