Невозможно добавить несколько кругов и маркеров карты. - PullRequest
0 голосов
/ 28 мая 2018

Я создаю приложение геозоны, и оно имеет интерфейс карты, чтобы пользователь мог видеть, где расположены геозоны.У меня проблема в том, что всякий раз, когда я загружаю координаты, он рисует только один круг и один маркер, но геозону можно вводить и закрывать по назначению.В настоящее время у меня нет физического устройства для тестирования, поэтому я полагаюсь на эмулятор.

Here is a GIF of it in action

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);
    }

}

1 Ответ

0 голосов
/ 28 мая 2018

Вы создаете GeofenceLatLngCoordinates = new ArrayList<>(); на каждой итерации цикла for.

Просто переместите эту строку перед циклом 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();
    }
}
...