使用多边形创建和监视地理围栏

时间:2018-11-16 02:06:40

标签: polygon android-maps geofencing android-geofence

我正在尝试使用多边形来实现Geofencing系统。基本上,如果用户进入地理围栏区域,则该用户应收到通知。经过大量研究后,我只能使用Circular找到Geofence。到目前为止,我已经实现了该系统,但是如果有人输入在地图中绘制的多边形,则它仅通过圆形进行监视。如果以前做过Polygon Geonfencing,请帮助我

这是我用来绘制多边形的代码

    private void drawGeofence() {
    Log.d(TAG, "drawGeofence()");

    polyLatLng = new ArrayList<>(  );
    polyLatLng.add( new LatLng( 6.895450, 79.852170 ) ); // Should match last point
    polyLatLng.add( new LatLng(6.897287, 79.859544));
    polyLatLng.add( new LatLng( 6.905271, 79.862609 ) );
    polyLatLng.add( new LatLng( 6.906114, 79.858998 ) );
    polyLatLng.add( new LatLng( 6.911808, 79.856206 ) );
    polyLatLng.add( new LatLng( 6.912200, 79.851381 ) );
    polyLatLng.add( new LatLng( 6.911627, 79.849621 ) );
    polyLatLng.add( new LatLng( 6.910965, 79.848073 ) );
    polyLatLng.add( new LatLng( 6.895450, 79.852170 ) );              // Should match first point

    Log.i(TAG, "computeArea " + SphericalUtil.computeArea(polyLatLng));

    map.addPolygon(new PolygonOptions()
            .addAll(polyLatLng)
            .strokeColor(Color.BLACK)
            .strokeWidth( 4 )
            .fillColor(0x220000FF));
}

这是我的地理围栏代码,仅在圆形区域内跟踪

 private static final float GEOFENCE_RADIUS = 1006.3975694699f; // in meters
private void startGeofence() {
    Log.i(TAG, "startGeofence()");
    if( geoFenceMarker != null ) {

        // create geofence
        Geofence geofence = createGeofence( 6.904254, 79.853798, GEOFENCE_RADIUS );
        GeofencingRequest geofenceRequest = createGeofenceRequest( geofence );
        addGeofence( geofenceRequest );
    } else {
        Log.e(TAG, "Geofence marker is null");
    }
}

// Create a Geofence
private Geofence createGeofence( double lat, double lng, float radius ) {
    Log.d(TAG, "createGeofence");

    return new Geofence.Builder()
            .setRequestId(GEOFENCE_REQ_ID)
            .setCircularRegion( lat, lng, radius)
            .setExpirationDuration( GEO_DURATION )
            .setTransitionTypes( Geofence.GEOFENCE_TRANSITION_ENTER
                    | Geofence.GEOFENCE_TRANSITION_EXIT )
            .build();
}enter code here

0 个答案:

没有答案