使用您自己的監視器 using-your-monitor

您也可以使用監控服務,並使用Places擴充功能API與Places服務整合。

註冊地理圍欄

如果您決定使用監控服務,請完成下列步驟,註冊目前位置周圍的POI地理圍欄:

iOS

在iOS中,完成下列步驟:

  1. 將從iOS核心位置服務取得的位置更新傳遞到Places擴充功能。

  2. 使用 getNearbyPointsOfInterest 放置擴充功能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];
        }];
    }
    
  3. 從取得的擷取資訊 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

  1. 將從Google Play服務或Android位置服務取得的位置更新傳遞到Places擴充功能。

  2. 使用 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);
                }
            });
        }
    };
    
  3. 從取得的擷取資料 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中,呼叫 processGeofenceEvent 將API放入 CLLocationManager 委派。 此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