我正在创建一个地理围栏应用程序,它有一个地图界面供用户查看地理围栏的位置。我遇到的问题是每当我加载坐标时,它只绘制一个圆圈和一个标记,但地理围栏可以按预期输入和退出。这时,我没有要测试的物理设备,所以我依赖于模拟器。
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);
}
}
答案 0 :(得分:1)
您正在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();
}
}