By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,478 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Android how add more geofences?

P: 1
I write applications in which geofences on the map will be displayed. For now, I managed to create one geofences which is displayed on the map. My question is how to convert the code so that you can display more than one geofences? I'm new to this topic, could someone help me, explain how to do it? Below I put two classes that I use.

MainActivity:
Expand|Select|Wrap|Line Numbers
  1. public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  2.  
  3.         private static final String TAG = "MainActivity";
  4.         private static final int REQUEST_LOCATION_PERMISSION_CODE = 101;
  5.         private GoogleMap googleMap;
  6.         protected ArrayList<Geofence> mGeofenceList;
  7.         private GeofencingRequest geofencingRequest;
  8.         private GoogleApiClient googleApiClient;
  9.         private boolean isMonitoring = false;
  10.         private MarkerOptions markerOptions;
  11.         private Marker currentLocationMarker;
  12.         private PendingIntent pendingIntent;
  13.         @Override
  14.         protected void onCreate(Bundle savedInstanceState) {
  15.             super.onCreate(savedInstanceState);
  16.             setContentView(R.layout.activity_main);
  17.             SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  18.                     .findFragmentById(R.id.map);
  19.             mapFragment.getMapAsync(this);
  20.             mGeofenceList = new ArrayList<Geofence>();
  21.             googleApiClient = new GoogleApiClient.Builder(this)
  22.                     .addApi(LocationServices.API)
  23.                     .addConnectionCallbacks(this)
  24.                     .addOnConnectionFailedListener(this).build();
  25.             if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  26.                     != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  27.                 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_PERMISSION_CODE);
  28.             }
  29.         }
  30.  
  31.         @NonNull
  32.         private Geofence getGeofence() {
  33.             LatLng latLng = Constants.AREA_LANDMARKS.get(Constants.GEOFENCE_ID_STAN_UNI);
  34.             return new Geofence.Builder()
  35.                     .setRequestId(Constants.GEOFENCE_ID_STAN_UNI)
  36.                     .setExpirationDuration(Geofence.NEVER_EXPIRE)
  37.                     .setCircularRegion(latLng.latitude, latLng.longitude, Constants.GEOFENCE_RADIUS_IN_METERS)
  38.                     .setNotificationResponsiveness(1000)
  39.                     .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
  40.                     .build();
  41.         }
  42.         @Override
  43.         protected void onResume() {
  44.             super.onResume();
  45.             int response = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this);
  46.             if (response != ConnectionResult.SUCCESS) {
  47.                 Log.d(TAG, "Google Play Service Not Available");
  48.                 GoogleApiAvailability.getInstance().getErrorDialog(MainActivity.this, response, 1).show();
  49.             } else {
  50.                 Log.d(TAG, "Google play service available");
  51.             }
  52.         }
  53.  
  54.         @Override
  55.         public void onMapReady(GoogleMap googleMap) {
  56.  
  57.             if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  58.                 return;
  59.             }
  60.  
  61.             this.googleMap = googleMap;
  62.             LatLng latLng = Constants.AREA_LANDMARKS.get(Constants.GEOFENCE_ID_STAN_UNI);
  63.             googleMap.addMarker(new MarkerOptions().position(latLng).title("Stanford University"));
  64.             googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17f));
  65.             googleMap.setMyLocationEnabled(true);
  66.             Circle circle = googleMap.addCircle(new CircleOptions()
  67.                     .center(new LatLng(latLng.latitude, latLng.longitude))
  68.                     .radius(Constants.GEOFENCE_RADIUS_IN_METERS)
  69.                     .strokeColor(Color.RED)
  70.                     .strokeWidth(4f));
  71.  
  72.         }
  73.     }
The second class is Constants, in which I store the latitude and longitude of geofences.

Expand|Select|Wrap|Line Numbers
  1. package com.app.androidkt.geofencing;
  2. import com.google.android.gms.maps.model.LatLng;
  3. import java.util.HashMap;
  4.  
  5.     public class Constants {
  6.  
  7.  
  8.         public static final String GEOFENCE_ID_STAN_UNI = "STAN_UNI";
  9.         public static final float GEOFENCE_RADIUS_IN_METERS = 100;
  10.  
  11.         /**
  12.          * Map for storing information about stanford university in the Stanford.
  13.          */
  14.         public static final HashMap<String, LatLng> AREA_LANDMARKS = new HashMap<String, LatLng>();
  15.         static {
  16.             // stanford university.
  17.             AREA_LANDMARKS.put(GEOFENCE_ID_STAN_UNI, new LatLng(37.427025, -122.170425));
  18.         }
  19.     }
1 Week Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.