Skip to content
Permalink
Browse files

Merge branch 'feature/#1294_2'

# Conflicts:
#	OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/SampleFactory.java
  • Loading branch information...
spyhunter99 committed Sep 3, 2019
2 parents bd6df43 + f6288f3 commit a4a4c74fb5413223905ed89f1b052ed4b965ec27
@@ -38,6 +38,7 @@
import org.osmdroid.samplefragments.data.SampleSimpleLocation;
import org.osmdroid.samplefragments.data.SampleSpeechBalloon;
import org.osmdroid.samplefragments.events.SampleAnimateToWithOrientation;
import org.osmdroid.samplefragments.events.SampleMapCenterOffset;
import org.osmdroid.samplefragments.events.SampleSnappable;
import org.osmdroid.samplefragments.tileproviders.SampleTileStates;
import org.osmdroid.samplefragments.data.SampleWithMinimapItemizedOverlayWithFocus;
@@ -308,6 +309,7 @@ private SampleFactory() {
mSamples.add(SampleAnimateToWithOrientation.class);
mSamples.add(SampleMapSnapshot.class);
mSamples.add(SampleSpeechBalloon.class);
mSamples.add(SampleMapCenterOffset.class);
mSamples.add(SampleSnappable.class);
}

@@ -144,7 +144,7 @@ public void run() {
});
}
}, MapSnapshot.INCLUDE_FLAG_UPTODATE, mapTileProvider, mOverlays,
new Projection(zoom, mMapSize, mMapSize, pDataRegion.getBox().getCenterWithDateLine(), 0, true, true));
new Projection(zoom, mMapSize, mMapSize, pDataRegion.getBox().getCenterWithDateLine(), 0, true, true, 0, 0));
mMapSnapshots.put(key, mapSnapshot);
new Thread(mapSnapshot).start(); // TODO use AsyncTask, Executors instead?
}
@@ -0,0 +1,135 @@
package org.osmdroid.samplefragments.events;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;

import org.osmdroid.R;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.Projection;
import org.osmdroid.views.overlay.Marker;
import org.osmdroid.views.overlay.Overlay;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
* @author Fabrice Fontaine
* @since 6.1.1
*/
public class SampleMapCenterOffset extends SampleMapEventListener {

private final int mOffsetX = 0;
private final int mOffsetY = 200;
private final Paint mPaint = new Paint();

private int mIndex;
private Timer t = new Timer();
private boolean alive = true;
private final List<GeoPoint> mList = new ArrayList<>();

@Override
public String getSampleTitle() {
return "Animate To with Map Center Offset";
}

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

final Drawable drawable = getResources().getDrawable(R.drawable.marker_default);

mList.add(new GeoPoint(38.8977, -77.0365)); // white house
mList.add(new GeoPoint(38.8719, -77.0563)); // pentagon
mList.add(new GeoPoint(38.8895, -77.0353)); // washington monument

for (final GeoPoint geoPoint : mList) {
final Marker startMarker = new Marker(mMapView);
startMarker.setPosition(geoPoint);
startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
startMarker.setIcon(drawable);
mMapView.getOverlays().add(startMarker);
}

mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(5);

mMapView.getOverlays().add(new Overlay() {

@Override
public void draw(Canvas pCanvas, Projection pProjection) {
mMapView.getProjection().save(pCanvas, false, true);
final float centerX = pCanvas.getWidth() / 2f;
final float centerY = pCanvas.getHeight() / 2f;
pCanvas.drawLine(centerX, centerY, centerX + mOffsetX, centerY + mOffsetY, mPaint);
mMapView.getProjection().restore(pCanvas, true);
}
});

mMapView.setMapCenterOffset(mOffsetX, mOffsetY);
mMapView.post(new Runnable() {
@Override
public void run() {
show();
}
});
}

@Override
public void onResume() {
super.onResume();
alive=true;
//some explanation here.
//we using a timer task with a delayed start up to move the map around. during CI tests
//this fragment can crash the app if you navigate away from the fragment before the initial fire
final TimerTask task = new TimerTask() {
@Override
public void run() {
runTask();
}
};

t = new Timer();
t.schedule(task, 4000, 4000);
}

@Override
public void onPause() {
super.onPause();
alive = false;
if (t != null)
t.cancel();
t = null;
}


private void runTask() {
if (!alive)
return;
if (getActivity() == null) {
return;
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mMapView == null || getActivity() == null) {
return;
}
show();
}
});
}

private void show() {
show(mIndex ++);
}

private void show(final int pIndex) {
final double zoom = 12.5;
final GeoPoint geoPoint = mList.get(pIndex % mList.size());
mMapView.getController().animateTo(geoPoint, zoom, null);
}
}
@@ -186,6 +186,14 @@
*/
private boolean mDestroyModeOnDetach = true;

/**
* @since 6.1.1
* The map center used to be projected into the screen center.
* Now we have a possible offset from the screen center; default offset is [0, 0].
*/
private int mMapCenterOffsetX;
private int mMapCenterOffsetY;

// ===========================================================
// Constructors
// ===========================================================
@@ -553,7 +561,8 @@ public double zoomToBoundingBox(final BoundingBox pBoundingBox, final boolean pA
nextZoom, getWidth(), getHeight(),
center,
getMapOrientation(),
isHorizontalMapRepetitionEnabled(), isVerticalMapRepetitionEnabled());
isHorizontalMapRepetitionEnabled(), isVerticalMapRepetitionEnabled(),
getMapCenterOffsetX(), getMapCenterOffsetY());
final Point point = new Point();
final double longitude = pBoundingBox.getCenterLongitude();
projection.toPixels(new GeoPoint(pBoundingBox.getActualNorth(), longitude), point);
@@ -1894,5 +1903,27 @@ public TilesOverlay getMapOverlay() {
public void setDestroyMode(final boolean pOnDetach) {
mDestroyModeOnDetach = pOnDetach;
}

/**
* @since 6.1.1
*/
public int getMapCenterOffsetX() {
return mMapCenterOffsetX;
}

/**
* @since 6.1.1
*/
public int getMapCenterOffsetY() {
return mMapCenterOffsetY;
}

/**
* @since 6.1.1
*/
public void setMapCenterOffset(final int pMapCenterOffsetX, final int pMapCenterOffsetY) {
mMapCenterOffsetX = pMapCenterOffsetX;
mMapCenterOffsetY = pMapCenterOffsetY;
}
}

@@ -61,14 +61,22 @@

private final TileSystem mTileSystem;

/**
* @since 6.1.1
*/
private final int mMapCenterOffsetX;
private final int mMapCenterOffsetY;

Projection(MapView mapView) {
this(
mapView.getZoomLevelDouble(), mapView.getIntrinsicScreenRect(null),
mapView.getExpectedCenter(),
mapView.getMapScrollX(), mapView.getMapScrollY(),
mapView.getMapOrientation(),
mapView.isHorizontalMapRepetitionEnabled(), mapView.isVerticalMapRepetitionEnabled(),
mapView.getTileSystem());
MapView.getTileSystem(),
mapView.getMapCenterOffsetX(),
mapView.getMapCenterOffsetY());
}

/**
@@ -79,11 +87,14 @@ public Projection(
final GeoPoint pCenter,
final long pScrollX, final long pScrollY,
final float pOrientation,
boolean horizontalWrapEnabled, boolean verticalWrapEnabled,
final TileSystem pTileSystem) {
final boolean pHorizontalWrapEnabled, final boolean pVerticalWrapEnabled,
final TileSystem pTileSystem,
final int pMapCenterOffsetX, final int pMapCenterOffsetY) {
mMapCenterOffsetX = pMapCenterOffsetX;
mMapCenterOffsetY = pMapCenterOffsetY;
mZoomLevelProjection = pZoomLevel;
this.horizontalWrapEnabled = horizontalWrapEnabled;
this.verticalWrapEnabled = verticalWrapEnabled;
horizontalWrapEnabled = pHorizontalWrapEnabled;
verticalWrapEnabled = pVerticalWrapEnabled;
mTileSystem = pTileSystem;
mMercatorMapSize = TileSystem.MapSize(mZoomLevelProjection);
mTileSize = TileSystem.getTileSize(mZoomLevelProjection);
@@ -106,14 +117,16 @@ public Projection(
final double pZoomLevel, final int pWidth, final int pHeight,
final GeoPoint pCenter,
final float pOrientation,
final boolean pHorizontalWrapEnabled, final boolean pVerticalWrapEnabled) {
final boolean pHorizontalWrapEnabled, final boolean pVerticalWrapEnabled,
final int pMapCenterOffsetX, final int pMapCenterOffsetY) {
this(
pZoomLevel, new Rect(0, 0, pWidth, pHeight),
pCenter,
0, 0,
pOrientation,
pHorizontalWrapEnabled, pVerticalWrapEnabled,
MapView.getTileSystem());
MapView.getTileSystem(),
pMapCenterOffsetX, pMapCenterOffsetY);
}

/**
@@ -125,7 +138,8 @@ public Projection getOffspring(final double pZoomLevel, final Rect pScreenRect)
mCurrentCenter, 0, 0,
mOrientation,
horizontalWrapEnabled, verticalWrapEnabled,
mTileSystem);
mTileSystem,
0, 0); // 0 looks like the most relevant value
}

public double getZoomLevel() {
@@ -584,14 +598,14 @@ public RectL getMercatorViewPort(final RectL pReuse) {
* @since 6.0.0
*/
public int getScreenCenterX() {
return (mIntrinsicScreenRectProjection.right + mIntrinsicScreenRectProjection.left) / 2;
return (mIntrinsicScreenRectProjection.right + mIntrinsicScreenRectProjection.left) / 2 + mMapCenterOffsetX;
}

/**
* @since 6.0.0
*/
public int getScreenCenterY() {
return (mIntrinsicScreenRectProjection.bottom + mIntrinsicScreenRectProjection.top) / 2;
return (mIntrinsicScreenRectProjection.bottom + mIntrinsicScreenRectProjection.top) / 2 + mMapCenterOffsetY;
}

/**
@@ -655,10 +669,7 @@ public void restore(final Canvas pCanvas, final boolean pForce) {
private void refresh() {
// of course we could write mIntrinsicScreenRectProjection.centerX() and centerY()
// but we should keep writing it that way (cf. ProjectionTest)
fromPixels(
(mIntrinsicScreenRectProjection.left + mIntrinsicScreenRectProjection.right) / 2,
(mIntrinsicScreenRectProjection.top + mIntrinsicScreenRectProjection.bottom) / 2,
mCurrentCenter);
fromPixels(getScreenCenterX(), getScreenCenterY(), mCurrentCenter);
IGeoPoint neGeoPoint = fromPixels(
mIntrinsicScreenRectProjection.right, mIntrinsicScreenRectProjection.top, null, true);
final TileSystem tileSystem = org.osmdroid.views.MapView.getTileSystem();
@@ -260,7 +260,8 @@ private Projection getRandomProjection(
pOffsetX, pOffsetY,
getRandomOrientation(),
true, true,
tileSystem);
tileSystem
, 0, 0);
}

private Projection getRandomProjection(final double pZoomLevel) {
@@ -284,7 +285,8 @@ public void test_conversionFromPixelsToPixels() {
0L, 0L,
0,
false, false,
tileSystem
tileSystem,
0, 0
);

final Point inputPoint = new Point(0, 0);

0 comments on commit a4a4c74

Please sign in to comment.
You can’t perform that action at this time.