Skip to content

Commit

Permalink
Merge pull request #1371 from osmdroid/bug/#1299_3
Browse files Browse the repository at this point in the history
bug/#1299_3 - demo for "Snappable" feature
  • Loading branch information
monsieurtanuki committed Jul 15, 2019
2 parents 12977ee + e6d012f commit 7b4bfbd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.osmdroid.samplefragments.data.SampleSimpleFastPointOverlay;
import org.osmdroid.samplefragments.data.SampleSimpleLocation;
import org.osmdroid.samplefragments.events.SampleAnimateToWithOrientation;
import org.osmdroid.samplefragments.events.SampleSnappable;
import org.osmdroid.samplefragments.tileproviders.SampleTileStates;
import org.osmdroid.samplefragments.data.SampleWithMinimapItemizedOverlayWithFocus;
import org.osmdroid.samplefragments.data.SampleWithMinimapItemizedOverlayWithScale;
Expand Down Expand Up @@ -305,6 +306,7 @@ private SampleFactory() {
mSamples.add(SampleTileStates.class);
mSamples.add(SampleAnimateToWithOrientation.class);
mSamples.add(SampleMapSnapshot.class);
mSamples.add(SampleSnappable.class);
}

public void addSample(Class<? extends BaseSampleFragment> clz) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.osmdroid.samplefragments.events;

import android.graphics.Point;

import org.osmdroid.api.IMapView;
import org.osmdroid.api.IProjection;
import org.osmdroid.samplefragments.BaseSampleFragment;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.Overlay;

/**
* @since 6.1.0
* @author Fabrice Fontaine
*/
public class SampleSnappable extends BaseSampleFragment {

// cf. https://en.wikipedia.org/wiki/Gare_de_Perpignan
private final GeoPoint MAP_CENTER = new GeoPoint(42.696111, 2.879444);

@Override
public String getSampleTitle() {
return "Snappable";
}

class MyOverlay extends Overlay implements Overlay.Snappable {

@Override
public boolean onSnapToItem(int x, int y, Point snapPoint, IMapView mapView) {
final IProjection projection = mapView.getProjection();
projection.toPixels(MAP_CENTER, snapPoint);
return true;
}
}

@Override
protected void addOverlays() {
super.addOverlays();

mMapView.getOverlayManager().add(new MyOverlay());
mMapView.post(new Runnable() {
@Override
public void run() {
mMapView.getController().setZoom(14.);
mMapView.setExpectedCenter(MAP_CENTER);
}
});
}
}

0 comments on commit 7b4bfbd

Please sign in to comment.