Använda din egen skärm using-your-monitor

Du kan också använda dina övervakningstjänster och integrera med Platstjänst genom att använda API:erna för Platstillägg.

Registrerar geofence

Om du bestämmer dig för att använda dina övervakningstjänster registrerar du geofences of the POIs runt din aktuella plats genom att utföra följande steg:

iOS

Gör så här i iOS:

  1. Skicka platsuppdateringar som hämtats från iOS bastjänst till Platser-tillägget.

  2. Använd API:t för tillägget getNearbyPointsOfInterest för att hämta arrayen med ACPPlacesPoi objekt runt den aktuella platsen.

    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. Extrahera informationen från de ACPPlacesPOI insamlade objekten och börja övervaka dessa 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. Skicka platsuppdateringar från Google Play eller Android positioneringstjänster till platstillägget.

  2. Använd API:t getNearbyPointsOfInterest Platser för tillägg för att få en lista över PlacesPoi-objekt runt den aktuella platsen.

    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. Extrahera data från de PlacesPOI hämtade objekten och börja övervaka dessa 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)
    }
    

Anrop av API:t getNearbyPointsOfInterest resulterar i ett nätverksanrop som hämtar platsen runt den aktuella platsen.

IMPORTANT
Du bör anropa API:t sparsamt eller endast när det finns en betydande platsändring av användaren.

Publicera geofence-händelser

iOS

Anropa API:t processGeofenceEvent för platser i CLLocationManager-delegaten i iOS. Detta API meddelar dig om användaren har angivit eller avslutat en viss region.

- (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

I Android anropar du metoden processGeofence tillsammans med lämplig övergångshändelse i Geofence-sändningsmottagaren. Du kanske vill strukturera listan över geofences som tagits emot för att förhindra dubblettposter/-utträden.

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