第1行: | 第1行: | ||
− | <div id="examples_center"> | + | <div id="examples_center"><!-- 页面标题 --><div class="title-hd">地理围栏</div><!-- 标题下子标题及简介,可有多个 --><div class="title-mi">功能场景</div><div class="wrap-mi">本示例展示了如何根据自身业务需要,创建不同种类和模式的地理围栏,用于车辆区域提醒、智能活动推送等场景。</div><!-- --></div><div class="demos_wrap"><div class="header"><div class="andiord-header active">Android</div><div class="ios-header">iOS</div></div><div class="andiord-wrap wrap"><div class="wrap-wp"><div class="wrap_left"><div class="left-video"><div class="img-bg-and"></div><div class="phone-android"><div class="video-wrap infor"><div class="video-control"></div><!-- 安卓视频位置 (不写协议)--><pre>//mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/地理围栏.mp4</pre> |
− | + | </div><div class="video-icon-and"><div class="play play-and"></div><div class="stop stop-and"></div></div></div><div class="progressWrap-hide-and"></div><div id="videoControls-and"><div id="progressWrap-and"><div id="playProgress-and"></div></div></div></div><div class="left-qrCode"><!-- 安卓二维码位置 --><div class="qrCode-and">1590746640|//mapopen-website-wiki.bj.bcebos.com/demos/newqrcodes/地理围栏.png</div><div>扫码体验</div></div></div><div class="wrap_right"><div class="wrap_right_nav"><div class="wrap_right_nav_l"><div>使用产品</div><div><!-- 安卓使用产品下选项,可有多个 --><!-- 产品1|/index.php?title=test --><div class="documentA">Android定位SDK|/index.php?title=android-locsdk</div><div>></div><div class="documentA">地理围栏|/index.php?title=android-locsdk/guide/addition-func/loc-geofence</div></div></div><div class="wrap_right_nav_r">下载源码</div><!-- 安卓源码地址 --><div class="github-url-and">//mapopen-website-wiki.bj.bcebos.com/demos/location/GeoFenceDemo.zip</div></div><div class="wrap_table"><div class="table_nav"><div class="left-icon"></div><div>核心接口</div></div><div class="table_wrap"><div class="table_wrap_title"><div>类</div><div>接口</div><div>描述</div><div>备注</div></div><!-- 表格接口,可有多个 --><div class="table_wrap_con"><div class="table_wrap_w"><div><div class="table-wrap-className">GeoFenceClient</div></div><div><div>addGeoFence(final String keyword, String poiType,final String city, int size,String customId);</div></div><div><div>添加围栏(POI行政区划检索围栏)</div></div><div><div>定位SDK V8.2.0 之后支持</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">GeoFenceClient</div></div><div><div>setActivateAction(int action)</div></div><div><div>设置当前创建的围栏的触发行为</div></div><div><div>定位SDK V8.2.0 之后支持</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">GeoFenceClient</div></div><div><div>removeGeoFence()</div></div><div><div>清除所有围栏</div></div><div><div>定位SDK V8.2.0 之后支持</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">GeoFenceListener</div></div><div><div>onGeoFenceCreateFinished(List<GeoFence> geoFenceList,int errorCode)</div></div><div><div>监听围栏状态回调 geoFenceList :本次创建的围栏列表(创建成功才有,不成功为空)errorCode:错误码 customId:本次操作对应的自定义业务ID(如果添加的时候没有设置customId,回调中也不会有)</div></div><div><div>定位SDK V8.2.0 之后支持</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BaiduMap</div></div><div><div>addOverlay(OverlayOptions options)</div></div><div><div>向地图添加一个 Overlay</div></div><div><div>地图SDK</div></div></div></div></div></div></div></div><div class="pre-nav"><div class="left-icon"></div><div>核心代码</div></div><div class="pre-nav" style="margin-left: 10px;"><div>示例Demo展示的是(圆形围栏,多边形围栏,POI行政区划检索围栏,POI圆形区域检索围栏,行政区域围栏)多种添加围栏添加方式。</div></div><div class="pre-nav"><div>1.添加围栏</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>JAVA</div></div><!-- 安卓示例代码位置 --><div class="pre-wrap-w pre-wrap-w-and"><pre class="prettyprint prettyprinted pre-and">/** | |
− | + | ||
− | + | ||
− | + | ||
− | </div> | + | |
− | <div class="demos_wrap"> | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | /** | + | |
* 添加围栏 | * 添加围栏 | ||
*/ | */ | ||
第194行: | 第34行: | ||
} | } | ||
</pre> | </pre> | ||
− | + | <div class="pre-btn"><div class="pre-btn-copy-and">复制</div><div class="pre-btn-cb-and">深色</div></div><div class="success">复制成功</div></div></div><div class="pre-nav"><div>2.监听围栏状态回调</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>JAVA</div></div><!-- 安卓示例代码位置 --><div class="pre-wrap-w pre-wrap-w-and"><pre class="prettyprint prettyprinted pre-and">List<GeoFence> fenceList = new ArrayList<>(); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | List<GeoFence> fenceList = new ArrayList<>(); | + | |
@Override | @Override | ||
public void onGeoFenceCreateFinished(final List<GeoFence> geoFenceList, | public void onGeoFenceCreateFinished(final List<GeoFence> geoFenceList, | ||
第227行: | 第50行: | ||
} | } | ||
</pre> | </pre> | ||
− | + | <div class="pre-btn"><div class="pre-btn-copy-and">复制</div><div class="pre-btn-cb-and">深色</div></div><div class="success">复制成功</div></div></div><div class="pre-nav"><div>3.在地图上绘制围栏</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>JAVA</div></div><!-- 安卓示例代码位置 --><div class="pre-wrap-w pre-wrap-w-and"><pre class="prettyprint prettyprinted pre-and">private void drawFence(GeoFence fence) { | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | private void drawFence(GeoFence fence) { | + | |
switch (fence.getType()) { | switch (fence.getType()) { | ||
case GeoFence.TYPE_ROUND: | case GeoFence.TYPE_ROUND: | ||
第311行: | 第117行: | ||
List<LatLng> lst = new ArrayList<>(); | List<LatLng> lst = new ArrayList<>(); | ||
− | for (DPoint point : pointList) { | + | for (DPoint point : pointList) { |
lst.add(new LatLng(point.getLatitude(), point.getLongitude())); | lst.add(new LatLng(point.getLatitude(), point.getLongitude())); | ||
boundsBuilder.include( | boundsBuilder.include( | ||
第321行: | 第127行: | ||
.stroke(new Stroke(5, 0xE66495ED))); | .stroke(new Stroke(5, 0xE66495ED))); | ||
− | if (polygonPoints != null && polygonPoints.size() > 0) { | + | if (polygonPoints != null && polygonPoints.size() > 0) { |
polygonPoints.clear(); | polygonPoints.clear(); | ||
} | } | ||
第327行: | 第133行: | ||
} | } | ||
</pre> | </pre> | ||
− | + | <div class="pre-btn"><div class="pre-btn-copy-and">复制</div><div class="pre-btn-cb-and">深色</div></div><div class="success">复制成功</div></div></div><!-- <div class="more-nav"> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
<div class="left-icon"></div> | <div class="left-icon"></div> | ||
<div>更多功能</div> | <div>更多功能</div> | ||
</div> | </div> | ||
− | <div class="more-wrap"> --> | + | <div class="more-wrap"> --><!-- 安卓更多功能下选项 --><!-- 点标记动画|/index.php?title=test --><!-- <div> |
− | + | ||
− | + | ||
− | + | ||
<div class="point"></div> | <div class="point"></div> | ||
<div class="documentA">点标记动画|/index.php?title=test</div> | <div class="documentA">点标记动画|/index.php?title=test</div> | ||
第354行: | 第149行: | ||
<div class="documentA">添加文字和信息窗|/index.php?title=test</div> | <div class="documentA">添加文字和信息窗|/index.php?title=test</div> | ||
</div> | </div> | ||
− | </div> --> | + | </div> --></div><div class="ios-wrap wrap" style="display: none;"><div class="wrap-wp"><div class="wrap_left"><div class="left-video"><div class="img-bg-ios"></div><div class="phone-ios"><div class="video-wrap infor"><div class="video-control"></div><!-- ios视频位置 --><pre>//mapopen-website-wiki.bj.bcebos.com/demos/iosVideos/location/地理围栏@1x.mp4</pre> |
− | + | </div><div class="video-icon-ios"><div class="play play-ios"></div><div class="stop stop-ios"></div></div></div><div class="progressWrap-hide-ios"></div><div id="videoControls-ios"><div id="progressWrap-ios"><div id="playProgress-ios"></div></div></div></div><div class="left-qrCode iosqrCode"><!-- ios二维码位置 --><div class="qrCode-ios">1590746640|//mapopen-website-wiki.bj.bcebos.com/demos/ioscodes/地理围栏@1xios.png</div><div>扫码体验</div></div></div><div class="wrap_right"><div class="wrap_right_nav"><div class="wrap_right_nav_l"><div>使用产品</div><div><!-- IOS使用产品下选项,可有多个 --><!-- 产品1|/index.php?title=test --><div class="documentA">iOS定位SDK|/index.php?title=ios-locsdk</div><!-- --><div>></div><div class="documentA">地理围栏|/index.php?title=ios-locsdk/guide/addition-func/geofence</div></div></div><div class="wrap_right_nav_r">下载源码</div><!-- IOS源码地址 --><div class="github-url-ios">//mapopen-website-wiki.bj.bcebos.com/demos/location/GeoFenceSDKDemo.zip</div></div><div class="wrap_table"><div class="table_nav"><div class="left-icon"></div><div>核心接口</div></div><div class="table_wrap"><div class="table_wrap_title"><div>类</div><div>接口</div><div>描述</div><div>备注</div></div><!-- ios表格接口内容,可有多个 --><!-- 表格接口,可有多个 --><div class="table_wrap_con"><div class="table_wrap_w"><div><div class="table-wrap-className">BMKGeoFenceManager</div></div><div><div>- (void)addCircleRegionForMonitoringWithCenter:(CLLocationCoordinate2D)center radius:(CLLocationDistance)radius coorType:(BMKLocationCoordinateType)type customID:(NSString * _Nullable)customID;</div></div><div><div>添加一个圆形围栏</div></div><div><div>定位sdk</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKGeoFenceManager</div></div><div><div>- (void)addPolygonRegionForMonitoringWithCoordinates:(CLLocationCoordinate2D * _Nonnull)coordinates count:(NSInteger)count coorType:(BMKLocationCoordinateType)type customID:(NSString * _Nullable)customID;</div></div><div><div>根据经纬度坐标数据添加一个闭合的多边形围栏,点与点之间按顺序尾部相连, 第一个点与最后一个点相连</div></div><div><div>定位sdk</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKGeoFenceManager</div></div><div><div>- (void)removeAllGeoFenceRegions;</div></div><div><div>移除所有围栏</div></div><div><div>定位sdk</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKGeoFenceManager</div></div><div><div>- (void)BMKGeoFenceManager:(BMKGeoFenceManager * _Nonnull)manager didAddRegionForMonitoringFinished:(NSArray <BMKGeoFenceRegion *> * _Nullable)regions customID:(NSString * _Nullable)customID error:(NSError * _Nullable)error;</div></div><div><div>添加地理围栏完成后的回调,成功与失败都会调用</div></div><div><div>定位sdk</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKGeoFenceManager</div></div><div><div>- (void)BMKGeoFenceManager:(BMKGeoFenceManager * _Nonnull)manager didGeoFencesStatusChangedForRegion:(BMKGeoFenceRegion * _Nullable)region customID:(NSString * _Nullable)customID error:(NSError * _Nullable)error;</div></div><div><div>地理围栏状态改变时回调,当围栏状态的值发生改变,定位失败都会调用</div></div><div><div>定位sdk</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKLocationManager</div></div><div><div>- (void)startUpdatingLocation;</div></div><div><div>开始连续定位。用来获取当前位置</div></div><div><div>定位sdk</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKMapView</div></div><div><div>-(void)updateLocationData:(BMKUserLocation *)userLocation;</div></div><div><div>动态更新我的位置数据</div></div><div><div>定位sdk</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKMapView</div></div><div><div>- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay;</div></div><div><div>生成与地理围栏对应的overlay</div></div><div><div>地图sdk</div></div></div></div></div></div></div></div><div class="pre-nav"><div class="left-icon"></div><div>核心代码</div></div><div class="pre-nav"><div>1.配置地理围栏管理类</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>Objective-C</div></div><div class="pre-wrap-w pre-wrap-w-ios code_container"><!-- ios示例代码位置 --><pre class="prettyprint prettyprinted pre-ios">_geofenceManager = [[BMKGeoFenceManager alloc] init]; | |
− | + | /// 指定定位是否会被系统自动暂停。默认为NO。 | |
− | + | _geofenceManager.pausesLocationUpdatesAutomatically = NO; | |
− | + | /// 是否允许后台定位。 | |
− | + | _geofenceManager.allowsBackgroundLocationUpdates = YES; | |
− | + | /// 需要进行通知的行为 | |
− | + | _geofenceManager.activeAction = BMKGeoFenceActiveActionInside; | |
− | + | /// 代理 | |
− | + | geofenceManager.delegate = self; | |
− | + | ||
− | + | </pre> | |
− | + | <div class="pre-btn"><div class="pre-btn-copy-ios">复制</div><div class="pre-btn-cb-ios">深色</div></div><div class="success">复制成功</div></div></div><div class="pre-nav"><div>2.添加地理围栏</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>Objective-C</div></div><div class="pre-wrap-w pre-wrap-w-ios code_container"><!-- ios示例代码位置 --><pre class="prettyprint prettyprinted pre-ios">//添加圆形围栏 | |
− | + | [self.geofenceManager addCircleRegionForMonitoringWithCenter:@”圆形围栏中心点坐标” radius:@”圆形围栏半径” coorType:BMKLocationCoordinateTypeBMK09LL customID:@”自定义业务”]; | |
− | + | // 添加多边形围栏 | |
− | + | [self.geofenceManager addPolygonRegionForMonitoringWithCoordinates:@”多边形围栏边界坐标点数组,至少三个” count:@”坐标点数量” coorType:BMKLocationCoordinateTypeBMK09LL customID: @”自定义业务”]; | |
− | + | ||
− | + | 注意:添加围栏的同时可添加和围栏形状相同的overlay让围栏可见,具体实现参照demo或地图overlay文档 | |
− | + | ||
− | + | </pre> | |
− | + | <div class="pre-btn"><div class="pre-btn-copy-ios">复制</div><div class="pre-btn-cb-ios">深色</div></div><div class="success">复制成功</div></div></div><div class="pre-nav"><div>3.实现地理围栏状态改变回调代理方法</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>Objective-C</div></div><div class="pre-wrap-w pre-wrap-w-ios code_container"><!-- ios示例代码位置 --><pre class="prettyprint prettyprinted pre-ios">- (void)BMKGeoFenceManager:(BMKGeoFenceManager * _Nonnull)manager didGeoFencesStatusChangedForRegion:(BMKGeoFenceRegion * _Nullable)region customID:(NSString * _Nullable)customID error:(NSError * _Nullable)error { | |
− | + | if (error) { | |
− | + | NSLog(@"geofence error = %@", error); | |
− | + | return; | |
− | + | } | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | NSLog(@"geofence %@ status = %ld", region.customID, region.fenceStatus); | |
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | switch(region.fenceStatus) { | |
− | + | case BMKGeoFenceRegionStatusInside: | |
− | + | [self showMsg:@"地理围栏状态:进入地理围栏"]; | |
− | + | break; | |
− | + | case BMKGeoFenceRegionStatusStayed: | |
− | + | [self showMsg:@"地理围栏状态:停留在地理围栏"]; | |
− | + | break; | |
− | + | case BMKGeoFenceRegionStatusOutside: | |
− | + | [self showMsg:@"地理围栏状态:离开地理围栏"]; | |
− | + | break; | |
− | + | default: | |
− | + | [self showMsg:@"地理围栏状态:围栏状态未知"]; | |
− | + | break; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
} | } | ||
− | + | ||
− | + | ||
</pre> | </pre> | ||
− | + | <div class="pre-btn"><div class="pre-btn-copy-ios">复制</div><div class="pre-btn-cb-ios">深色</div></div><div class="success">复制成功</div></div></div><div class="pre-nav"><div>4.移除地理围栏</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>Objective-C</div></div><div class="pre-wrap-w pre-wrap-w-ios code_container"><!-- ios示例代码位置 --><pre class="prettyprint prettyprinted pre-ios">// 移除所有围栏 | |
− | + | [self.geofenceManager removeAllGeoFenceRegions]; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | / | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</pre> | </pre> | ||
− | + | <div class="pre-btn"><div class="pre-btn-copy-ios">复制</div><div class="pre-btn-cb-ios">深色</div></div><div class="success">复制成功</div></div></div></div></div> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </div> | + |
2020年9月24日 (四) 20:53的版本
地理围栏
功能场景
本示例展示了如何根据自身业务需要,创建不同种类和模式的地理围栏,用于车辆区域提醒、智能活动推送等场景。
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(); } }
复制
深色
复制成功