首页 > 解决方案 > 用户进入谷歌地图特定区域时如何调用方法

问题描述

我是编程的初学者。我正在使用 Firebase 数据库在 Java 中创建一个 Android 游戏,例如使用 Google 地图的“捉迷藏”。第一队创建提示(文字或照片),第二队使用这些提示找到第一队。每个用户都有自己的手机。

我正在努力解决的问题是:当第一个团队用户创建提示时,例如。拍一张照片,我想在地图上创建一个以用户位置为中心,半径为 50m 的区域。当其他团队的用户进入该区域时,会调用正确的方法并接收来自数据库的下一个提示。

我尝试过使用 GeoFences 并设法创建一个地理围栏并在地图上画一个圆圈(只是为了检查它是否工作)。现在,我不知道如何在二队球员设备上设置一些监听器,以便在他们进入这个地理围栏时进行监听。当他们这样做时,我需要调用一个从数据库下载提示的方法。

创建地理围栏的代码:

private static final long GEO_DURATION = 24 * 60 * 60 * 1000;
private static final String GEOFENCE_REQ_ID = "GeofenceIDe";
private static final float GEOFENCE_RADIUS = 50;
private final int GEOFENCE_REQ_CODE = 0;

private void startGeofence() {
    latLng = new LatLng(52.337506, 20.419784);
    Geofence geofence = createGeofence(latLng);
    GeofencingRequest geofenceRequest = createGeofenceRequest( geofence );
    addGeofence( geofenceRequest );
}

private Geofence createGeofence(LatLng latLng) {
    return new Geofence.Builder()
            .setRequestId(GEOFENCE_REQ_ID)
            .setCircularRegion( latLng.latitude, latLng.longitude, GEOFENCE_RADIUS)
            .setExpirationDuration( GEO_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 createGeofencePendingIntent() {

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

private void addGeofence(GeofencingRequest request) {
    LocationServices.GeofencingApi.addGeofences(
            mGoogleApiClient,
            request,
            createGeofencePendingIntent()
    ).setResultCallback(this);
}

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

private Circle geoFenceLimits;
private void drawGeofence() {
    if ( geoFenceLimits != null )
        geoFenceLimits.remove();
    CircleOptions circleOptions = new CircleOptions()
            .center( latLng)
            .strokeColor(Color.argb(50, 70,70,70))
            .fillColor( Color.argb(100, 150,150,150) )
            .radius( GEOFENCE_RADIUS );
    geoFenceLimits = mMap.addCircle( circleOptions );
}***

我在 Intent 中调用“GameSummaryActivity”只是为了检查它是否可以工作。我的目标是调用一个方法,而不是一个活动。

任何想法如何实现?提前致谢!

标签: androidfirebaselocation

解决方案


推荐阅读