使用您自己的監視器 using-your-monitor
您也可以使用監控服務,並使用Places擴充功能API與Places服務整合。
註冊地理圍欄
如果您決定使用監控服務,請完成下列步驟,註冊目前位置周圍的POI地理圍欄:
iOS
在iOS中,完成下列步驟:
-
將從iOS核心位置服務取得的位置更新傳遞至Places擴充功能。
-
使用
getNearbyPointsOfInterest
Places擴充功能API取得目前位置周圍的ACPPlacesPoi
物件陣列。code language-objective-c - (void) locationManager: (CLLocationManager*) manager didUpdateLocations: (NSArray<CLLocation*>*) locations { [ACPPlaces getNearbyPointsOfInterest:currentLocation limit:10 callback: ^ (NSArray<ACPPlacesPoi*>* _Nullable nearbyPoi) { [self startMonitoringGeoFences:nearbyPoi]; }]; }
-
從取得的
ACPPlacesPOI
物件中擷取資訊,並開始監視這些POI。code language-objective-c - (void) startMonitoringGeoFences: (NSArray*) newGeoFences { // verify if the device supports monitoring geofences // check for location permission for (ACPPlacesPoi * currentRegion in newGeoFences) { // make the circular region CLLocationCoordinate2D center = CLLocationCoordinate2DMake(currentRegion.latitude, currentRegion.longitude); CLCircularRegion* currentCLRegion = [[CLCircularRegion alloc] initWithCenter:center radius:currentRegion.radius identifier:currentRegion.identifier]; currentCLRegion.notifyOnExit = YES; currentCLRegion.notifyOnEntry = YES; // start monitoring the new region [_locationManager startMonitoringForRegion:currentCLRegion]; } }
Android
-
將從Google Play服務或Android定位服務取得的位置更新傳遞到Places擴充功能。
-
使用
getNearbyPointsOfInterest
Places擴充功能API取得目前位置周圍PlacesPoi
物件的清單。code language-java LocationCallback callback = new LocationCallback() { @Override public void onLocationResult(LocationResult locationResult) { super.onLocationResult(locationResult); Places.getNearbyPointsOfInterest(currentLocation, 10, new AdobeCallback<List<PlacesPOI>>() { @Override public void call(List<PlacesPOI> pois) { starMonitoringGeofence(pois); } }); } };
-
從取得的
PlacesPOI
物件擷取資料,並開始監視這些POI。code language-java private void startMonitoringFences(final List<PlacesPOI> nearByPOIs) { // check for location permission for (PlacesPOI poi : nearByPOIs) { final Geofence fence = new Geofence.Builder() .setRequestId(poi.getIdentifier()) .setCircularRegion(poi.getLatitude(), poi.getLongitude(), poi.getRadius()) .setExpirationDuration(Geofence.NEVER_EXPIRE) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build(); geofences.add(fence); } GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); builder.addGeofences(geofences); builder.build(); geofencingClient.addGeofences(builder.build(), geoFencePendingIntent) }
呼叫getNearbyPointsOfInterest
API會導致網路呼叫,取得目前位置周圍的位置。
IMPORTANT
您應該謹慎呼叫API,或僅在使用者發生重大位置變更時呼叫。
發佈地理圍欄事件
iOS
在iOS中,呼叫CLLocationManager
委派中的processGeofenceEvent
Places API。 此API會通知您使用者是否已進入或離開特定區域。
- (void) locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
[ACPPlaces processRegionEvent:region forRegionEventType:ACPRegionEventTypeEntry];
}
- (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
[ACPPlaces processRegionEvent:region forRegionEventType:ACPRegionEventTypeExit];
}
Android
在Android中,呼叫processGeofence
方法,同時在您的地理圍欄廣播接收器呼叫適當的轉換事件。 您可能想要組織收到的地理柵欄清單,以防止重複進入/退出。
void onGeofenceReceived(final Intent intent) {
// do appropriate validation steps for the intent
...
// get GeofencingEvent from intent
GeofencingEvent geoEvent = GeofencingEvent.fromIntent(intent);
// get the transition type (entry or exit)
int transitionType = geoEvent.getGeofenceTransition();
// validate your geoEvent and get the necessary Geofences from the list
List<Geofence> myGeofences = geoEvent.getTriggeringGeofences();
// process region events for your geofences
for (Geofence geofence : myGeofences) {
Places.processGeofence(geofence, transitionType);
}
}
recommendation-more-help
475fad96-f29f-4dca-a109-68bf0557e825