Skip to content

Commit

Permalink
Group layer implementation, closes mapsforge#860
Browse files Browse the repository at this point in the history
  • Loading branch information
devemux86 committed Jul 22, 2016
1 parent 24e29a0 commit a583420
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## New since 0.6.1

- Group layer implementation [#860](https://github.com/mapsforge/mapsforge/issues/860)
- Allow resources without location prefixes in render themes [#847](https://github.com/mapsforge/mapsforge/issues/847)
- SVG symbols customization [#858](https://github.com/mapsforge/mapsforge/issues/858)
- Deprecate water tiles rendering [#640](https://github.com/mapsforge/mapsforge/issues/640)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2016 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mapsforge.map.layer;

import org.mapsforge.core.graphics.Canvas;
import org.mapsforge.core.model.BoundingBox;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.Point;
import org.mapsforge.map.model.DisplayModel;

import java.util.ArrayList;
import java.util.List;

/**
* A layer which is a group of other layers.
*/
public class GroupLayer extends Layer {

/**
* The group of other layers.
*/
public final List<Layer> layers = new ArrayList<>();

public GroupLayer() {
super();
}

@Override
public void draw(BoundingBox boundingBox, byte zoomLevel, Canvas canvas, Point topLeftPoint) {
for (Layer layer : layers) {
layer.draw(boundingBox, zoomLevel, canvas, topLeftPoint);
}
}

@Override
public void onDestroy() {
for (Layer layer : layers) {
layer.onDestroy();
}
}

@Override
public boolean onLongPress(LatLong tapLatLong, Point layerXY, Point tapXY) {
for (int i = layers.size() - 1; i >= 0; i--) {
Layer layer = layers.get(i);
if (layer.onLongPress(tapLatLong, layerXY, tapXY)) {
return true;
}
}
return false;
}

@Override
public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
for (int i = layers.size() - 1; i >= 0; i--) {
Layer layer = layers.get(i);
if (layer.onTap(tapLatLong, layerXY, tapXY)) {
return true;
}
}
return false;
}

@Override
public synchronized void setDisplayModel(DisplayModel displayModel) {
super.setDisplayModel(displayModel);
for (Layer layer : layers) {
layer.setDisplayModel(displayModel);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 devemux86
* Copyright 2015-2016 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
Expand All @@ -25,6 +25,7 @@
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.Point;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.layer.GroupLayer;
import org.mapsforge.map.layer.Layer;
import org.mapsforge.map.layer.Layers;
import org.mapsforge.map.layer.overlay.Circle;
Expand All @@ -47,7 +48,7 @@
*/
public class PoiSearchViewer extends RenderTheme4 {
private static final String POI_FILE = Environment.getExternalStorageDirectory() + "/germany.poi";
private static final String POI_CATEGORY = "Embassies";
private static final String POI_CATEGORY = "Restaurants";

private static final Paint CIRCLE = Utils.createPaint(AndroidGraphicFactory.INSTANCE.createColor(128, 255, 0, 0), 0, Style.FILL);

Expand All @@ -71,7 +72,7 @@ private void onLongPress() {
// Clear overlays
Layers layers = this.mapView.getLayerManager().getLayers();
for (Layer layer : layers) {
if (layer instanceof Circle) {
if (layer instanceof GroupLayer) {
layers.remove(layer);
}
}
Expand Down Expand Up @@ -120,19 +121,23 @@ protected void onPostExecute(Collection<PointOfInterest> pointOfInterests) {
return;
}

GroupLayer groupLayer = new GroupLayer();
for (final PointOfInterest pointOfInterest : pointOfInterests) {
Circle circle = new FixedPixelCircle(pointOfInterest.getLatLong(), 16, CIRCLE, null) {
final Circle circle = new FixedPixelCircle(pointOfInterest.getLatLong(), 16, CIRCLE, null) {
@Override
public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
if (this.contains(layerXY, tapXY)) {
// GroupLayer does not have a position!
Point circleXY = activity.mapView.getMapViewProjection().toPixels(getPosition());
if (this.contains(circleXY, tapXY)) {
Toast.makeText(activity, pointOfInterest.getName(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
};
activity.mapView.getLayerManager().getLayers().add(circle);
groupLayer.layers.add(circle);
}
activity.mapView.getLayerManager().getLayers().add(groupLayer);
activity.redrawLayers();
}
}
Expand Down

0 comments on commit a583420

Please sign in to comment.