473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Android how add more geofences?

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.     }
Apr 14 '19 #1
0 3153

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

Similar topics

1
Nepomuk
by: Nepomuk | last post by:
Android development is a pretty broad field and no one article could ever hope to encompass everything there is to learn. For that reason, this article attempts to collect links to various tutorials,...
1
by: Niels Koomans | last post by:
$ make j4 ============================================ PLATFORM_VERSION_CODENAME=REL PLATFORM_VERSION=4.4 TARGET_PRODUCT=aosp_x86 TARGET_BUILD_VARIANT=eng TARGET_BUILD_TYPE=release...
0
by: Niels Koomans | last post by:
Hello dear developers, Today i tried to make a build for Android-x86 version Kitkat 4.4.2. I got a error during the end of build while creating system.img: + '' /bin/bash: / 2048 *...
0
by: WhatsTheQuestio | last post by:
At this point I know how to utilize Google Maps within Android but it always seems to take up the full window, there is an image below which shows what I'm attempting to accomplish (having a box...
0
by: 5402702 | last post by:
My question is do I need to have an android SDK package saved to start a new android project, even if I don't want to run it. And do I need the SDK to create an android virtual device in eclipse. My...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.