使用您自己的监视器 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