如何在Android中添加多个地理栅栏?

时间:2019-04-14 06:34:08

标签: android android-geofence

我编写了将在地图上显示地理围栏的应用程序。现在,我设法创建了一个在地图上显示的地理围栏。我的问题是如何转换代码,以便可以显示多个地理围栏?我是这个主题的新手,有人可以帮我解释如何做吗?下面我放置了两个我要使用的类。

MainActivity:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

        private static final String TAG = "MainActivity";
        private static final int REQUEST_LOCATION_PERMISSION_CODE = 101;
        private GoogleMap googleMap;
        protected ArrayList<Geofence> mGeofenceList;
        private GeofencingRequest geofencingRequest;
        private GoogleApiClient googleApiClient;
        private boolean isMonitoring = false;
        private MarkerOptions markerOptions;
        private Marker currentLocationMarker;
        private PendingIntent pendingIntent;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
            mGeofenceList = new ArrayList<Geofence>();
            googleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).build();
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_PERMISSION_CODE);
            }
        }

        @NonNull
        private Geofence getGeofence() {
            LatLng latLng = Constants.AREA_LANDMARKS.get(Constants.GEOFENCE_ID_STAN_UNI);
            return new Geofence.Builder()
                    .setRequestId(Constants.GEOFENCE_ID_STAN_UNI)
                    .setExpirationDuration(Geofence.NEVER_EXPIRE)
                    .setCircularRegion(latLng.latitude, latLng.longitude, Constants.GEOFENCE_RADIUS_IN_METERS)
                    .setNotificationResponsiveness(1000)
                    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                    .build();
        }
        @Override
        protected void onResume() {
            super.onResume();
            int response = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this);
            if (response != ConnectionResult.SUCCESS) {
                Log.d(TAG, "Google Play Service Not Available");
                GoogleApiAvailability.getInstance().getErrorDialog(MainActivity.this, response, 1).show();
            } else {
                Log.d(TAG, "Google play service available");
            }
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }

            this.googleMap = googleMap;
            LatLng latLng = Constants.AREA_LANDMARKS.get(Constants.GEOFENCE_ID_STAN_UNI);
            googleMap.addMarker(new MarkerOptions().position(latLng).title("Stanford University"));
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17f));
            googleMap.setMyLocationEnabled(true);
            Circle circle = googleMap.addCircle(new CircleOptions()
                    .center(new LatLng(latLng.latitude, latLng.longitude))
                    .radius(Constants.GEOFENCE_RADIUS_IN_METERS)
                    .strokeColor(Color.RED)
                    .strokeWidth(4f));

        }
    }

第二个类是常量,我在其中存储地理围栏的纬度和经度。

package com.app.androidkt.geofencing;
import com.google.android.gms.maps.model.LatLng;
import java.util.HashMap;

    public class Constants {


        public static final String GEOFENCE_ID_STAN_UNI = "STAN_UNI";
        public static final float GEOFENCE_RADIUS_IN_METERS = 100;

        /**
         * Map for storing information about stanford university in the Stanford.
         */
        public static final HashMap<String, LatLng> AREA_LANDMARKS = new HashMap<String, LatLng>();
        static {
            // stanford university.
            AREA_LANDMARKS.put(GEOFENCE_ID_STAN_UNI, new LatLng(37.427025, -122.170425));
        }
    }

我的应用程序如下所示: enter image description here

0 个答案:

没有答案