Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Geofencing avec Google Places

Geofencing avec Google Places

Présentation du Geofencing sur Android avec Google Places.

Robin Caroff

January 24, 2017

Transcript

33 slides
  1. GEOFENCING Dépendances Gradle dependencies { compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.google.android.gms:play-services:9.4.0' }a
  2. dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1' a

    compile 'com.google.android.gms:play-services-location:9.4.0' }a GEOFENCING Dépendances Gradle
  3. GEOFENCING dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1'

    a compile 'com.google.android.gms:play-services-location:9.4.0' compile 'com.google.android.gms:play-services-maps:9.4.0' compile 'com.google.android.gms:play-services-places:9.4.0' }a Dépendances Gradle
  4. PERMISSIONS @Override protected void onCreate(Bundle savedInstanceState) { ... checkPermissions(); }a

    private void checkPermissions() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); } else { goToMainActivity(); }b }c
  5. PERMISSIONS @Override protected void onCreate(Bundle savedInstanceState) { ... checkPermissions(); }a

    private void checkPermissions() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); } else { goToMainActivity(); }b }c
  6. GOOGLE API CLIENT protected synchronized void buildGoogleApiClient() { mGoogleApiClient =

    new GoogleApiClient .Builder(context) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .addApi(Places.GEO_DATA_API) .addApi(Places.PLACE_DETECTION_API) .build(); }
  7. GOOGLE API CLIENT @Override protected void onStart() { super.onStart(); mGoogleApiClient.connect();

    } @Override protected void onStop() { super.onStop(); mGoogleApiClient.disconnect(); }
  8. CREATE A GEOFENCE Geofence geofence = new Geofence.Builder() .setRequestId(STRING_ID) .setCircularRegion(

    LATITUDE, LONGITUDE, RADIUS_IN_METERS ) .setExpirationDuration(EXPIRATION_IN_MILLISECONDS) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build());
  9. CREATE A GEOFENCE Geofence geofence = new Geofence.Builder() .setRequestId(STRING_ID) .setCircularRegion(

    LATITUDE, LONGITUDE, RADIUS_IN_METERS ) .setExpirationDuration(EXPIRATION_IN_MILLISECONDS) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build());
  10. CREATE A GEOFENCE Geofence geofence = new Geofence.Builder() .setRequestId(STRING_ID) .setCircularRegion(

    LATITUDE, LONGITUDE, RADIUS_IN_METERS ) .setExpirationDuration(EXPIRATION_IN_MILLISECONDS) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build());
  11. CREATE A GEOFENCE Geofence geofence = new Geofence.Builder() .setRequestId(STRING_ID) .setCircularRegion(

    LATITUDE, LONGITUDE, RADIUS_IN_METERS ) .setExpirationDuration(EXPIRATION_IN_MILLISECONDS) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build());
  12. GEOFENCING REQUEST private GeofencingRequest getGeofencingRequest() { GeofencingRequest.Builder builder = new

    GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); builder.addGeofences(mGeofenceList); return builder.build(); }
  13. GEOFENCE INTENT private PendingIntent getGeofencePendingIntent() { if (mGeofencePendingIntent != null)

    { return mGeofencePendingIntent; } Intent intent = new Intent(this, GeofenceTransitionsIntentService.class); return PendingIntent.getService(this, 0, intent, PendingIntent. FLAG_UPDATE_CURRENT); }
  14. BEST PRACTICES ▸ Réduire l’utilisation de la batterie en changeant

    la ‘réactivité’ des notifications ▸ Optimiser les rayons de geofences ▸ Surveiller seulement quand c’est nécessaire ▸ Utiliser les transitions DWELL plutôt que ENTER

More Decks by Robin Caroff

Other Decks in Programming