无法添加多个圆圈和地图标记

Cannot add multiple circles and map markers

我正在创建一个地理围栏应用程序,它有一个地图界面供用户查看地理围栏所在的位置。我遇到的问题是,每当我加载坐标时,它只绘制一个圆圈和一个标记,但可以按预期进入和退出地理围栏。目前,我没有可以测试的物理设备,所以我依赖于模拟器。

private void loadGeofencesFromDatabase() {

    for (Map.Entry<String, LatLng> entry : Constants.BAY_AREA_LANDMARKS.entrySet()) {

        mGeofenceList.add(new Geofence.Builder()
                // Set the request ID of the geofence. This is a string to identify this
                // geofence.
                .setRequestId(entry.getKey())

                // Set the circular region of this geofence.
                .setCircularRegion(
                        entry.getValue().latitude,
                        entry.getValue().longitude,
                        Constants.GEOFENCE_RADIUS_IN_METERS
                )

                // Set the expiration duration of the geofence. This geofence gets automatically
                // removed after this period of time.
                .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)

                // Set the transition types of interest. Alerts are only generated for these
                // transition. We track entry and exit transitions in this sample.
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                        Geofence.GEOFENCE_TRANSITION_EXIT)

                // Create the geofence.
                .build());

        GeofenceLatLngCoordinates = new ArrayList<>();
        GeofenceLatLngCoordinates.add(new LatLng(entry.getValue().latitude, entry.getValue().longitude));

        GEOFENCE_RADIUS = Constants.GEOFENCE_RADIUS_IN_METERS;

        addGeofences();

    }
}

这是负责在地图上绘制圆圈和标记的方法。它从 ArrayList 中检索坐标,然后尝试使用 for 循环在地图上绘制圆和标记。

private void drawGeofence() {
    Log.d(TAG, "GEOFENCE: Drawing Geofence on Map");

    for (LatLng latLng : GeofenceLatLngCoordinates){

        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) )
                .strokeColor(Color.RED)
                .strokeWidth(5)
                .radius(GEOFENCE_RADIUS);
        geoFenceLimits = map.addCircle( circleOptions );

        MarkerOptions markerOptions = new MarkerOptions()
                .position(new LatLng(latLng.latitude, latLng.longitude))
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                .title("My Geofence Location");

        testMark = map.addMarker(markerOptions);
    }

}

您正在 for 循环的每次迭代中创建您的 GeofenceLatLngCoordinates = new ArrayList<>();

只需将那行移动到 for 循环之前:

private void loadGeofencesFromDatabase() {
    GeofenceLatLngCoordinates = new ArrayList<>();
    GEOFENCE_RADIUS = Constants.GEOFENCE_RADIUS_IN_METERS;

    for (Map.Entry<String, LatLng> entry : Constants.BAY_AREA_LANDMARKS.entrySet()) {
        mGeofenceList.add(new Geofence.Builder()
                // Set the request ID of the geofence. This is a string to identify this
                // geofence.
                .setRequestId(entry.getKey())

                // Set the circular region of this geofence.
                .setCircularRegion(
                        entry.getValue().latitude,
                        entry.getValue().longitude,
                        Constants.GEOFENCE_RADIUS_IN_METERS
                )

                // Set the expiration duration of the geofence. This geofence gets automatically
                // removed after this period of time.
                .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)

                // Set the transition types of interest. Alerts are only generated for these
                // transition. We track entry and exit transitions in this sample.
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                        Geofence.GEOFENCE_TRANSITION_EXIT)

                // Create the geofence.
                .build());

        GeofenceLatLngCoordinates.add(new LatLng(entry.getValue().latitude, entry.getValue().longitude));
        addGeofences();
    }
}