Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.maptest;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.Toast;
- import com.example.maptest.model.Geofence;
- import com.example.maptest.model.GeofenceClient;
- import com.example.maptest.model.GeofenceTransition;
- import com.mapbox.android.core.location.LocationEngine;
- import com.mapbox.android.core.location.LocationEngineProvider;
- import com.mapbox.mapboxsdk.geometry.LatLng;
- import java.util.ArrayList;
- import java.util.List;
- public class GeofenceActivity extends AppCompatActivity implements GeofenceTransition {
- private static final int RADIUS_IN_METERS = 20;
- private List<Geofence> geofences;
- private GeofenceClient geofenceClient;
- private LocationEngine engine;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_geofence);
- geofences = generateGeofences();
- engine = LocationEngineProvider.getBestLocationEngine(this);
- geofenceClient = new GeofenceClient(geofences, engine, this);
- geofenceClient.start();
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (geofenceClient.isRunning()) {
- geofenceClient.stop();
- }
- }
- @Override
- public void onGeofenceEnter(Geofence geofence) {
- Toast.makeText(this, "ENTERED GEOFENCE", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onGeofenceExit(Geofence geofence) {
- Toast.makeText(this, "EXITED GEOFENCE", Toast.LENGTH_SHORT).show();
- }
- private List<Geofence> generateGeofences() {
- List<Geofence> geofences = new ArrayList<>();
- for (int i = 0; i < 10; i++) {
- Geofence geofence = new Geofence.Builder()
- .addCenter(new LatLng())
- .addRadius(RADIUS_IN_METERS).build();
- geofences.add(geofence);
- }
- return geofences;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement