如何更改在多边形上的地理围栏中创建的圆?

时间:2019-04-15 10:00:46

标签: java android geofencing android-geofence

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

MainActivity:




package com.app.androidkt.geofencing;
import android.Manifest;
import android.app.PendingIntent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.Map;
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 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();
    getGeofence();
    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 void getGeofence()
  {
    for (Map.Entry<String, LatLng> entry : Constants.AREA_LANDMARKS.entrySet())
    {
      mGeofenceList.add(new Geofence.Builder()
          .setRequestId(entry.getKey())
          .setCircularRegion(
              entry.getValue().latitude,
              entry.getValue().longitude,
              Constants.GEOFENCE_RADIUS_IN_METERS
          )
          .setExpirationDuration(Geofence.NEVER_EXPIRE)
          .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
  protected void onStart()
  {
    super.onStart();
  }
  @override
  protected void onStop()
  {
    super.onStop();
  }
  @override
  protected void onDestroy()
  {
    super.onDestroy();
  }
  @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_UNI2);
    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));
  }
  @override
  public void onConnected(@nullable Bundle bundle)
  {
    Log.d(TAG, "Google Api Client Connected");
  }
  @override
  public void onConnectionSuspended(int i)
  {
    Log.d(TAG, "Google Connection Suspended");
  }
  @override
  public void onConnectionFailed(@nonnull ConnectionResult connectionResult)
  {
    Log.e(TAG, "Connection Failed:" + connectionResult.getErrorMessage());
  }
}

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


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;
  public static final HashMap<String, LatLng> AREA_LANDMARKS = new HashMap<String, LatLng>();

  static
  {
    // San Francisco International Airport.
    AREA_LANDMARKS.put(GEOFENCE_ID_STAN_UNI, new LatLng(37.783888, -122.4009012));
    // Googleplex.
    AREA_LANDMARKS.put(GEOFENCE_ID_STAN_UNI2, new LatLng(51.250689, 22.572479));
    // Test
    AREA_LANDMARKS.put(GEOFENCE_ID_STAN_UNI, new LatLng(37.621313, -122.378955));
  }
}

0 个答案:

没有答案