use of org.mapsforge.map.layer.overlay.Circle in project satstat by mvglasow.
the class MapSectionFragment method applyLocationProviderStyle.
/**
* Applies a style to the map overlays associated with a given location provider.
*
* This method changes the style (effectively, the color) of the circle and
* marker overlays. Its main purpose is to switch the color of the overlays
* between gray and the provider color.
*
* @param context The context of the caller
* @param provider The name of the location provider, as returned by
* {@link LocationProvider.getName()}.
* @param styleName The name of the style to apply. If it is null, the
* default style for the provider as returned by
* assignLocationProviderStyle() is applied.
*/
protected void applyLocationProviderStyle(Context context, String provider, String styleName) {
String sn = (styleName != null) ? styleName : assignLocationProviderStyle(provider);
Boolean isStyleChanged = !sn.equals(providerAppliedStyles.get(provider));
Boolean needsRedraw = false;
Resources res = context.getResources();
TypedArray style = res.obtainTypedArray(res.getIdentifier(sn, "array", context.getPackageName()));
// Circle layer
Circle circle = mapCircles.get(provider);
if (circle != null) {
circle.getPaintFill().setColor(style.getColor(Const.STYLE_FILL, R.color.circle_gray_fill));
circle.getPaintStroke().setColor(style.getColor(Const.STYLE_STROKE, R.color.circle_gray_stroke));
needsRedraw = isStyleChanged && circle.isVisible();
}
// Marker layer
Marker marker = mapMarkers.get(provider);
if (marker != null) {
Drawable drawable = style.getDrawable(Const.STYLE_MARKER);
Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
marker.setBitmap(bitmap);
needsRedraw = needsRedraw || (isStyleChanged && marker.isVisible());
}
if (needsRedraw) {
LayerManager manager = mapMap.getLayerManager();
if (manager != null)
manager.redrawLayers();
}
providerAppliedStyles.put(provider, sn);
style.recycle();
}
use of org.mapsforge.map.layer.overlay.Circle in project satstat by mvglasow.
the class MapSectionFragment method onLocationProvidersChanged.
/**
* Updates internal data structures when the user's selection of location providers has changed.
* @param providers The new set of location providers
*/
public void onLocationProvidersChanged(Set<String> providers) {
Context context = this.getContext();
List<String> allProviders = mainActivity.locationManager.getAllProviders();
ArrayList<String> removedProviders = new ArrayList<String>();
for (String pr : providerLocations.keySet()) if (!providers.contains(pr))
removedProviders.add(pr);
// remove cached locations and invalidators for providers which are no longer selected
for (String pr : removedProviders) {
providerLocations.remove(pr);
providerInvalidators.remove(pr);
}
// ensure there is a cached location for each chosen provider (can be null)
for (String pr : providers) {
if ((allProviders.indexOf(pr) >= 0) && !providerLocations.containsKey(pr)) {
Location location = new Location("");
providerLocations.put(pr, location);
}
}
// add overlays
updateLocationProviderStyles();
mapCircles = new HashMap<String, Circle>();
mapMarkers = new HashMap<String, Marker>();
Log.d(TAG, "Provider location cache: " + providerLocations.keySet().toString());
Layers layers = mapMap.getLayerManager().getLayers();
// remove all layers other than tile render layer from map
for (Layer layer : layers) if (!(layer instanceof TileRendererLayer) && !(layer instanceof TileDownloadLayer)) {
layer.onDestroy();
layers.remove(layer);
}
for (String pr : providers) {
// no invalidator for GPS, which is invalidated through GPS status
if ((!pr.equals(LocationManager.GPS_PROVIDER)) && (providerInvalidators.get(pr)) == null) {
final String provider = pr;
final Context ctx = context;
providerInvalidators.put(pr, new Runnable() {
private String mProvider = provider;
@Override
public void run() {
Location location = providerLocations.get(mProvider);
if (location != null)
markLocationAsStale(location);
applyLocationProviderStyle(ctx, mProvider, Const.LOCATION_PROVIDER_GRAY);
}
});
}
String styleName = assignLocationProviderStyle(pr);
LatLong latLong;
float acc;
boolean visible;
if ((providerLocations.get(pr) != null) && (providerLocations.get(pr).getProvider() != "")) {
latLong = new LatLong(providerLocations.get(pr).getLatitude(), providerLocations.get(pr).getLongitude());
if (providerLocations.get(pr).hasAccuracy())
acc = providerLocations.get(pr).getAccuracy();
else
acc = 0;
visible = true;
if (isLocationStale(providerLocations.get(pr)))
styleName = Const.LOCATION_PROVIDER_GRAY;
Log.d("MainActivity", pr + " has " + latLong.toString());
} else {
latLong = new LatLong(0, 0);
acc = 0;
visible = false;
Log.d("MainActivity", pr + " has no location, hiding");
}
// Circle layer
Resources res = context.getResources();
TypedArray style = res.obtainTypedArray(res.getIdentifier(styleName, "array", context.getPackageName()));
Paint fill = AndroidGraphicFactory.INSTANCE.createPaint();
float density = context.getResources().getDisplayMetrics().density;
fill.setColor(style.getColor(Const.STYLE_FILL, R.color.circle_gray_fill));
fill.setStyle(Style.FILL);
Paint stroke = AndroidGraphicFactory.INSTANCE.createPaint();
stroke.setColor(style.getColor(Const.STYLE_STROKE, R.color.circle_gray_stroke));
stroke.setStrokeWidth(Math.max(1.5f * density, 1));
stroke.setStyle(Style.STROKE);
Circle circle = new Circle(latLong, acc, fill, stroke);
mapCircles.put(pr, circle);
layers.add(circle);
circle.setVisible(visible);
// Marker layer
Drawable drawable = style.getDrawable(Const.STYLE_MARKER);
Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
Marker marker = new Marker(latLong, bitmap, 0, -bitmap.getHeight() * 10 / 24);
mapMarkers.put(pr, marker);
layers.add(marker);
marker.setVisible(visible);
style.recycle();
}
// move layers into view
updateMap();
}
use of org.mapsforge.map.layer.overlay.Circle in project satstat by mvglasow.
the class MapSectionFragment method onLocationChanged.
/**
* Called when a new location is found by a registered location provider.
* Stores the location and updates GPS display and map view.
*/
public void onLocationChanged(Location location) {
// location from that provider as stale
if (Double.isNaN(location.getLatitude()) || Double.isNaN(location.getLongitude())) {
markLocationAsStale(providerLocations.get(location.getProvider()));
applyLocationProviderStyle(this.getContext(), location.getProvider(), Const.LOCATION_PROVIDER_GRAY);
return;
}
if (providerLocations.containsKey(location.getProvider()))
providerLocations.put(location.getProvider(), new Location(location));
LatLong latLong = new LatLong(location.getLatitude(), location.getLongitude());
Circle circle = mapCircles.get(location.getProvider());
Marker marker = mapMarkers.get(location.getProvider());
if (circle != null) {
circle.setLatLong(latLong);
if (location.hasAccuracy()) {
circle.setVisible(true);
circle.setRadius(location.getAccuracy());
} else {
Log.d("MainActivity", "Location from " + location.getProvider() + " has no accuracy");
circle.setVisible(false);
}
}
if (marker != null) {
marker.setLatLong(latLong);
marker.setVisible(true);
}
applyLocationProviderStyle(this.getContext(), location.getProvider(), null);
Runnable invalidator = providerInvalidators.get(location.getProvider());
if (invalidator != null) {
providerInvalidationHandler.removeCallbacks(invalidator);
providerInvalidationHandler.postDelayed(invalidator, PROVIDER_EXPIRATION_DELAY);
}
// redraw, move locations into view and zoom out as needed
if ((circle != null) || (marker != null) || (invalidator != null))
updateMap();
}
Aggregations