如何在Android上创建Geofences?

时间:2018-01-19 10:38:52

标签: android android-geofence

我正在尝试弄清楚如何在Android上创建Geofences。我已经关注了几个教程,但我永远无法让它为我工作。

我的想法是,我想创建一个Geofence,然后使用Google Maps将其显示在地图上。然后,当用户进入地理围栏区域时,它将显示通知并执行某些操作。

我想要创建Geofences的唯一要求是用户是否位于该特定位置。让我们说他在西雅图市中心的当地星巴克。如果他在那个特定区域,他只能创建一个地理围栏。

我想知道的是如何在创建地理围栏时实现这第一步?即使这意味着必须重写大量代码。我需要做些什么才能做到这一点?

private void startGeofence() {
    Log.i(TAG, "startGeofence");
    Geofence geofence = createGeofence( geoFenceMarker.getPosition(), GEOFENCE_RADIUS);
    GeofencingRequest geofencingRequest = createGeofenceRequest(geofence);
    addGeofence(geofencingRequest);
}

private static final long GEOFENCE_DURATION = 60 * 60 * 1000;
private static final String GEOFENCE_REQ_ID = "My Geofence";
private static final float GEOFENCE_RADIUS = 100.0f;

private Geofence createGeofence(LatLng latLng, float radius) {

    return new Geofence.Builder()
            .setRequestId(GEOFENCE_REQ_ID)
            .setCircularRegion(latLng.latitude, latLng.longitude, radius)
            .setExpirationDuration(GEOFENCE_DURATION)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER
                    | Geofence.GEOFENCE_TRANSITION_EXIT)
            .build();


}

private GeofencingRequest createGeofenceRequest(Geofence geofence) {

    return new GeofencingRequest.Builder()
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
            .addGeofence(geofence)
            .build();
}

private PendingIntent geofencePendingIntent;
private final int GEOFENCE_REQ_CODE = 0;

private PendingIntent createGeofencePendingIntent() {
    if (geofencePendingIntent != null)
        return geofencePendingIntent;

    Intent intent = new Intent(this, GeofenceTransitionService.class);
    return PendingIntent.getService(this, GEOFENCE_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

private void addGeofence(GeofencingRequest request) {
    if (checkPermissions())
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }LocationServices.GeofencingApi.addGeofences(googleApiClient, request,
            createGeofencePendingIntent()).setResultCallback((ResultCallback<? super Status>) this);
}

public void onResult(@NonNull Status status) {
    if (status.isSuccess() ) {
        drawGeofence();
    }
    else {

    }
}

private LatLng latLng;
private Circle geofenceLimit;
private void drawGeofence() {
    if (geofenceLimit != null){
        geofenceLimit.remove();
    }

    CircleOptions circleOptions = new CircleOptions()
            .center(new LatLng(latLng.latitude, latLng.longitude))
            .strokeColor(Color.argb(50, 70,70,70))
            .fillColor( Color.argb(100, 150,150,150) )
            .radius( GEOFENCE_RADIUS );
    geofenceLimit = googleMap.addCircle( circleOptions );

}

0 个答案:

没有答案