org.osmdroid.views.Projection Java Examples

The following examples show how to use org.osmdroid.views.Projection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LinearRing.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * @since 6.0.0
 * Mandatory use before clipping.
 */
public void setClipArea(final Projection pProjection) {
	final double border = .1;
	final Rect rect = pProjection.getIntrinsicScreenRect();
	final int halfWidth = rect.width() / 2;
	final int halfHeight = rect.height() / 2;
	// People less lazy than me would do more refined computations for width and height
	// that include the map orientation: the covered area would be smaller but still big enough
	// Now we use the circle which contains the `MapView`'s 4 corners
	final double radius = Math.sqrt(halfWidth * halfWidth + halfHeight * halfHeight);
	// cf. https://github.com/osmdroid/osmdroid/issues/1528
	// People less lazy than me would not double the radius and rather fix the pixel coordinates:
	// using Projection.getScreenCenterX() and Y() would certainly make sense
	// instead of halfWidth and halfHeight, and that could improve performances
	// to have a smaller radius (in that case, not doubled)
	final double doubleRadius = 2 * radius;
	final int scaledRadius = (int) (doubleRadius * (1 + border));
	setClipArea(
			halfWidth - scaledRadius, halfHeight - scaledRadius,
			halfWidth + scaledRadius, halfHeight + scaledRadius
	);
	// TODO: Not sure if this is the correct approach 
	this.isHorizontalRepeating = pProjection.isHorizontalWrapEnabled();
	this.isVerticalRepeating = pProjection.isVerticalWrapEnabled();
}
 
Example #2
Source File: GroundOverlay4.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
protected void computeMatrix(final Projection pProjection) {
    final long topLeftCornerX = pProjection.getLongPixelXFromLongitude(mTopLeft.getLongitude());
    final long topLeftCornerY = pProjection.getLongPixelYFromLatitude(mTopLeft.getLatitude());
    final long topRightCornerX = pProjection.getLongPixelXFromLongitude(mTopRight.getLongitude());
    final long topRightCornerY = pProjection.getLongPixelYFromLatitude(mTopRight.getLatitude());
    final long bottomRightCornerX = pProjection.getLongPixelXFromLongitude(mBottomRight.getLongitude());
    final long bottomRightCornerY = pProjection.getLongPixelYFromLatitude(mBottomRight.getLatitude());
    final long bottomLeftCornerX = pProjection.getLongPixelXFromLongitude(mBottomLeft.getLongitude());
    final long bottomLeftCornerY = pProjection.getLongPixelYFromLatitude(mBottomLeft.getLatitude());

    mMatrixDst[0] = (float) topLeftCornerX;
    mMatrixDst[1] = (float) topLeftCornerY;
    mMatrixDst[2] = (float) topRightCornerX;
    mMatrixDst[3] = (float) topRightCornerY;
    mMatrixDst[4] = (float) bottomRightCornerX;
    mMatrixDst[5] = (float) bottomRightCornerY;
    mMatrixDst[6] = (float) bottomLeftCornerX;
    mMatrixDst[7] = (float) bottomLeftCornerY;

    getMatrix().setPolyToPoly(mMatrixSrc, 0, mMatrixDst, 0, 4);
}
 
Example #3
Source File: ProjectionTest.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * "The geo center of an offspring matches the geo center of the parent"
 */
@Test
public void testOffspringSameCenter() {
    final GeoPoint center = new GeoPoint(0., 0);
    final Point pixel = new Point();
    final int centerX = (mScreenRect.right + mScreenRect.left) / 2;
    final int centerY = (mScreenRect.bottom + mScreenRect.top) / 2;
    final int miniCenterX = (mMiniMapScreenRect.right + mMiniMapScreenRect.left) / 2;
    final int miniCenterY = (mMiniMapScreenRect.bottom + mMiniMapScreenRect.top) / 2;
    for (int zoomLevel = mMinZoomLevel + mMinimapZoomLevelDifference; zoomLevel <= mMaxZoomLevel; zoomLevel ++) {
        for (int i = 0; i < mNbIterations; i ++) {
            final Projection projection = getRandomProjection(zoomLevel);
            final Projection miniMapProjection = projection.getOffspring(zoomLevel - mMinimapZoomLevelDifference, mMiniMapScreenRect);

            projection.fromPixels(centerX, centerY, center);
            miniMapProjection.toPixels(center, pixel);
            Assert.assertEquals(miniCenterX, pixel.x);
            Assert.assertEquals(miniCenterY, pixel.y);
        }
    }
}
 
Example #4
Source File: MyLocationNewOverlay.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onSnapToItem(final int x, final int y, final Point snapPoint,
		final IMapView mapView) {
	if (this.mLocation != null) {
		Projection pj = mMapView.getProjection();
		pj.toPixels(mGeoPoint, mSnapPixel);
		snapPoint.x = mSnapPixel.x;
		snapPoint.y = mSnapPixel.y;
		final double xDiff = x - mSnapPixel.x;
		final double yDiff = y - mSnapPixel.y;
		boolean snap = xDiff * xDiff + yDiff * yDiff < 64;
		if (Configuration.getInstance().isDebugMode()) {
                   Log.d(IMapView.LOGTAG, "snap=" + snap);
		}
		return snap;
	} else {
		return false;
	}
}
 
Example #5
Source File: ItemizedOverlay.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * Draw a marker on each of our items. populate() must have been called first.<br>
 * <br>
 * The marker will be drawn twice for each Item in the Overlay--once in the shadow phase, skewed
 * and darkened, then again in the non-shadow phase. The bottom-center of the marker will be
 * aligned with the geographical coordinates of the Item.<br>
 * <br>
 * The order of drawing may be changed by overriding the getIndexToDraw(int) method. An item may
 * provide an alternate marker via its OverlayItem.getMarker(int) method. If that method returns
 * null, the default marker is used.<br>
 * <br>
 * The focused item is always drawn last, which puts it visually on top of the other items.<br>
 */
@Override
public void draw(final Canvas canvas, final Projection pj) {
	if (mPendingFocusChangedEvent && mOnFocusChangeListener != null)
		mOnFocusChangeListener.onFocusChanged(this, mFocusedItem);
	mPendingFocusChangedEvent = false;

	final int size = Math.min(this.mInternalItemList.size(), mDrawnItemsLimit);

	if (mInternalItemDisplayedList == null || mInternalItemDisplayedList.length != size) {
		mInternalItemDisplayedList = new boolean[size];
	}

	/* Draw in backward cycle, so the items with the least index are on the front. */
	for (int i = size - 1; i >= 0; i--) {
		final Item item = getItem(i);
		if (item == null) {
			continue;
		}

           pj.toPixels(item.getPoint(), mCurScreenCoords);
		calculateItemRect(item, mCurScreenCoords, itemRect);

		mInternalItemDisplayedList[i] = onDrawItem(canvas,item, mCurScreenCoords, pj);
       }
   }
 
Example #6
Source File: MapSnapshot.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
public MapSnapshot(final MapSnapshotable pMapSnapshotable,
                   final int pIncludeFlags,
                   final MapTileProviderBase pTileProvider,
                   final List<Overlay> pOverlays,
                   final Projection pProjection) {
    mMapSnapshotable = pMapSnapshotable;
    mIncludeFlags = pIncludeFlags;
    mTileProvider = pTileProvider;
    mOverlays = pOverlays;
    mProjection = pProjection;
    mProjection.getMercatorViewPort(mViewPort);
    mTilesOverlay = new TilesOverlay(mTileProvider, null);
    mTilesOverlay.setHorizontalWrapEnabled(mProjection.isHorizontalWrapEnabled());
    mTilesOverlay.setVerticalWrapEnabled(mProjection.isVerticalWrapEnabled());
    mHandler = new MapSnapshotHandler(this);
    mTileProvider.getTileRequestCompleteHandlers().add(mHandler);
}
 
Example #7
Source File: PolyOverlayWithIW.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(final Canvas pCanvas, final Projection pProjection) {
	if (!isVisible(pProjection)) {
		return;
	}

	if (mDowngradeMaximumPixelSize > 0) {
		if (!isWorthDisplaying(pProjection)) {
			if (mDowngradeDisplay) {
				displayDowngrade(pCanvas, pProjection);
			}
			return;
		}
	}

	if (mPath != null) {
		drawWithPath(pCanvas, pProjection);
	} else {
		drawWithLines(pCanvas, pProjection);
	}
}
 
Example #8
Source File: ProjectionTest.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * @since 6.0.2
 * cf. https://github.com/osmdroid/osmdroid/issues/929
 */
@Test
public void test_conversionFromPixelsToPixels() {
    for (int zoomLevel = mMinZoomLevel; zoomLevel <= mMaxZoomLevel; zoomLevel ++) {
        final Projection projection = new Projection(
                zoomLevel,
                new Rect(0, 0, 1080, 1536),
                new GeoPoint(0.0, 0.0),
                0L, 0L,
                0,
                false, false,
                tileSystem,
                0, 0
        );

        final Point inputPoint = new Point(0, 0);
        final GeoPoint geoPoint = (GeoPoint) projection.fromPixels(inputPoint.x, inputPoint.y);
        final Point outputPoint = projection.toPixels(geoPoint, null);

        Assert.assertEquals(inputPoint.x, outputPoint.x);
        Assert.assertEquals(inputPoint.y, outputPoint.y);
    }
}
 
Example #9
Source File: PolyOverlayWithIW.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * @since 6.2.0
 */
private boolean isWorthDisplaying(final Projection pProjection) {
	final BoundingBox boundingBox = getBounds();
	pProjection.toPixels(new GeoPoint(boundingBox.getLatNorth(), boundingBox.getLonEast()), mDowngradeTopLeft);
	pProjection.toPixels(new GeoPoint(boundingBox.getLatSouth(), boundingBox.getLonWest()), mDowngradeBottomRight);
	final double worldSize = pProjection.getWorldMapSize();
	final long right = Math.round(mOutline.getCloserValue(mDowngradeTopLeft.x, mDowngradeBottomRight.x, worldSize));
	final long bottom = Math.round(mOutline.getCloserValue(mDowngradeTopLeft.y, mDowngradeBottomRight.y, worldSize));
	if (Math.abs(mDowngradeTopLeft.x - mDowngradeBottomRight.x) < mDowngradeMaximumPixelSize) {
		return false;
	}
	if (Math.abs(mDowngradeTopLeft.x - right) < mDowngradeMaximumPixelSize) {
		return false;
	}
	if (Math.abs(mDowngradeTopLeft.y - mDowngradeBottomRight.y) < mDowngradeMaximumPixelSize) {
		return false;
	}
	if (Math.abs(mDowngradeTopLeft.y - bottom) < mDowngradeMaximumPixelSize) {
		return false;
	}
	return true;
}
 
Example #10
Source File: PolyOverlayWithIW.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
   * Used to be in both {@link Polyline} and {@link Polygon}
   * Default listener for a single tap event on a Poly:
   * set the infowindow at the tapped position, and open the infowindow (if any).
   * @return true if tapped
   */
  @Override
  public boolean onSingleTapConfirmed(final MotionEvent pEvent, final MapView pMapView){
final Projection projection = pMapView.getProjection();
final GeoPoint eventPos = (GeoPoint) projection.fromPixels((int)pEvent.getX(), (int)pEvent.getY());
      final GeoPoint geoPoint;
      if (mPath != null) {
	final boolean tapped = contains(pEvent);
	if (tapped) {
		geoPoint = eventPos;
	} else {
		geoPoint = null;
	}
} else {
	final double tolerance = mOutlinePaint.getStrokeWidth() * mDensity * mDensityMultiplier;
	geoPoint = getCloseTo(eventPos, tolerance, pMapView);
}
      if (geoPoint != null) {
          return click(pMapView, geoPoint);
      }
      return false;
  }
 
Example #11
Source File: SampleTileStates.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
@Override
protected void addOverlays() {
    super.addOverlays();

    final Bitmap ok = ((BitmapDrawable)getResources().getDrawable(R.drawable.baseline_done_outline_black_36)).getBitmap();
    final Bitmap ko = ((BitmapDrawable)getResources().getDrawable(R.drawable.twotone_warning_black_36)).getBitmap();
    mMapView.getOverlayManager().add(new Overlay() {
        @Override
        public void draw(Canvas c, Projection projection) {
            final Bitmap bitmap = mOk ? ok : ko;
            c.drawBitmap(bitmap, c.getWidth() / 2 - bitmap.getWidth() / 2, c.getHeight() / 2 - bitmap.getHeight() / 2, null);
        }
    });
    mMapView.getMapOverlay().getTileStates().getRunAfters().add(new Runnable() {
        @Override
        public void run() {
            mTextView.setText(mTileStates.toString());
            mOk = mTileStates.isDone() && mTileStates.getTotal() == mTileStates.getUpToDate();
        }
    });
}
 
Example #12
Source File: IconOverlay.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * Draw the icon.
 */
@Override
public void draw(Canvas canvas, Projection pj) {
    if (mIcon == null)
        return;
    if (mPosition == null)
        return;

    pj.toPixels(mPosition, mPositionPixels);
    int width = mIcon.getIntrinsicWidth();
    int height = mIcon.getIntrinsicHeight();
    Rect rect = new Rect(0, 0, width, height);
    rect.offset(-(int)(mAnchorU*width), -(int)(mAnchorV*height));
    mIcon.setBounds(rect);

    mIcon.setAlpha((int) (mAlpha * 255));

    float rotationOnScreen = (mFlat ? -mBearing : pj.getOrientation()-mBearing);
    drawAt(canvas, mIcon, mPositionPixels.x, mPositionPixels.y, false, rotationOnScreen);
}
 
Example #13
Source File: LinearRing.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * Feed the path with the segments corresponding to the GeoPoint pairs
 * projected using pProjection and clipped into a "reasonable" clip area
 * In most cases (Polygon without holes, Polyline) the offset parameter will be null.
 * In the case of a Polygon with holes, the first path will use a null offset.
 * Then this method will return the pixel offset computed for this path so that
 * the path is in the best possible place on the map:
 * the center of all pixels is as close to the screen center as possible
 * Then, this computed offset must be injected into the buildPathPortion for each hole,
 * in order to have the main polygon and its holes at the same place on the map.
 * @return the initial offset if not null, or the computed offset
 */
PointL buildPathPortion(final Projection pProjection,
						final PointL pOffset,
						final boolean pStorePoints){
	final int size = mOriginalPoints.size();
	if (size < 2) { // nothing to paint
		return pOffset;
	}
       computeProjected();
       computeDistances();
	final PointL offset;
	if (pOffset != null) {
		offset = pOffset;
	} else {
		offset = new PointL();
		getBestOffset(pProjection, offset);
	}
	mSegmentClipper.init();
	clipAndStore(pProjection, offset, mClosed, pStorePoints, mSegmentClipper);
	mSegmentClipper.end();
	if (mClosed) {
	    mPath.close();
       }
	return offset;
}
 
Example #14
Source File: ProjectionTest.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
private Projection getRandomProjection(
        final double pZoomLevel, final GeoPoint pGeoPoint,
        final long pOffsetX, final long pOffsetY) {
    return new Projection(pZoomLevel, mScreenRect,
            pGeoPoint,
            pOffsetX, pOffsetY,
            getRandomOrientation(),
            true, true,
            tileSystem
            , 0, 0);
}
 
Example #15
Source File: GroundOverlay2.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(final Canvas pCanvas, final Projection pProjection) {
	if(mImage == null) {
		return;
	}
	computeMatrix(pProjection);
	pCanvas.drawBitmap(getImage(), getMatrix(), getPaint());
}
 
Example #16
Source File: CompassOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
protected void drawCompass(final Canvas canvas, final float bearing, final Rect screenRect) {
    final Projection proj = mMapView.getProjection();

    float centerX;
    float centerY;
    if (mInCenter) {
        final Rect rect = proj.getScreenRect();
        centerX = rect.exactCenterX();
        centerY = rect.exactCenterY();
    } else {
        centerX = mCompassCenterX * mScale;
        centerY = mCompassCenterY * mScale;
    }

    mCompassMatrix.setTranslate(-mCompassFrameCenterX, -mCompassFrameCenterY);
    mCompassMatrix.postTranslate(centerX, centerY);

    proj.save(canvas, false, true);
    canvas.concat(mCompassMatrix);
    canvas.drawBitmap(mCompassFrameBitmap, 0, 0, sSmoothPaint);
    proj.restore(canvas, true);

    mCompassMatrix.setRotate(-bearing, mCompassRoseCenterX, mCompassRoseCenterY);
    mCompassMatrix.postTranslate(-mCompassRoseCenterX, -mCompassRoseCenterY);
    mCompassMatrix.postTranslate(centerX, centerY);

    proj.save(canvas, false, true);
    canvas.concat(mCompassMatrix);
    canvas.drawBitmap(mCompassRoseBitmap, 0, 0, sSmoothPaint);
    proj.restore(canvas, true);
}
 
Example #17
Source File: GroundOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(final Canvas pCanvas, final Projection pProjection) {
    if(mImage == null) {
        return;
    }
    computeMatrix(pProjection);
    pCanvas.drawBitmap(mImage, mMatrix, mPaint);
}
 
Example #18
Source File: CompassOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas c, Projection pProjection) {
    if (isCompassEnabled() && !Float.isNaN(mAzimuth)) {
        drawCompass(c, mMode * (mAzimuth + mAzimuthOffset + getDisplayOrientation()), pProjection
            .getScreenRect());
    }
}
 
Example #19
Source File: MinimapOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas c, Projection pProjection) {
	if (!setViewPort(c, pProjection)) {
		return;
	}

	// Draw a solid background where the minimap will be drawn with a 2 pixel inset
	pProjection.save(c, false, true);
	c.drawRect(
			getCanvasRect().left - 2, getCanvasRect().top - 2,
			getCanvasRect().right + 2, getCanvasRect().bottom + 2, mPaint);

	super.drawTiles(c, getProjection(), getProjection().getZoomLevel(), mViewPort);
	pProjection.restore(c, true);
}
 
Example #20
Source File: LinearRing.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * @since 6.0.0
 * 
 */
private void clipAndStore(final Projection pProjection, final PointL pOffset,
						  final boolean pClosePath, final boolean pStorePoints,
						  final SegmentClipper pSegmentClipper) {
	mPointsForMilestones.clear();
	final double powerDifference = pProjection.getProjectedPowerDifference();
	final PointL projected = new PointL();
	final PointL point = new PointL();
	final PointL first = new PointL();
	for (int i = 0 ; i < mProjectedPoints.length ; i += 2) {
		projected.set(mProjectedPoints[i], mProjectedPoints[i + 1]);
		pProjection.getLongPixelsFromProjected(projected, powerDifference, false, point);
		final long x = point.x + pOffset.x;
		final long y = point.y + pOffset.y;
		if (pStorePoints) {
			mPointsForMilestones.add(x, y);
		}
		if (pSegmentClipper != null) {
			pSegmentClipper.add(x, y);
		}
		if (i == 0) {
			first.set(x, y);
		}
	}
	if (pClosePath) {
		if (pSegmentClipper != null) {
			pSegmentClipper.add(first.x, first.y);
		}
		if (pStorePoints) {
			mPointsForMilestones.add(first.x, first.y);
		}
	}
}
 
Example #21
Source File: MinimapOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean setViewPort(final Canvas pCanvas, final Projection pProjection) {
	final double zoomLevel = pProjection.getZoomLevel() - getZoomDifference();
	if (zoomLevel < mTileProvider.getMinimumZoomLevel()) {
		return false;
	}

	final int left = pCanvas.getWidth() - getPadding() - getWidth();
	final int top = pCanvas.getHeight() - getPadding() - getHeight();
	setCanvasRect(new Rect(left, top, left + getWidth(), top + getHeight()));
	setProjection(pProjection.getOffspring(zoomLevel, getCanvasRect()));
	getProjection().getMercatorViewPort(mViewPort);
	return true;
}
 
Example #22
Source File: DirectedLocationOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(final Canvas c, final Projection pj) {
	if (this.mLocation != null) {
		pj.toPixels(this.mLocation, screenCoords);

		if (this.mShowAccuracy && this.mAccuracy > 10) {
			final float accuracyRadius = pj.metersToEquatorPixels(this.mAccuracy);
			/* Only draw if the DirectionArrow doesn't cover it. */
			if (accuracyRadius > 8) {
				/* Draw the inner shadow. */
				this.mAccuracyPaint.setAntiAlias(false);
				this.mAccuracyPaint.setAlpha(30);
				this.mAccuracyPaint.setStyle(Style.FILL);
				c.drawCircle(screenCoords.x, screenCoords.y, accuracyRadius,
						this.mAccuracyPaint);

				/* Draw the edge. */
				this.mAccuracyPaint.setAntiAlias(true);
				this.mAccuracyPaint.setAlpha(150);
				this.mAccuracyPaint.setStyle(Style.STROKE);
				c.drawCircle(screenCoords.x, screenCoords.y, accuracyRadius,
						this.mAccuracyPaint);
			}
		}

		/*
		 * Rotate the direction-Arrow according to the bearing we are driving. And draw it to
		 * the canvas.
		 */
		this.directionRotater.setRotate(this.mBearing, DIRECTION_ARROW_CENTER_X,
				DIRECTION_ARROW_CENTER_Y);
		final Bitmap rotatedDirection = Bitmap.createBitmap(DIRECTION_ARROW, 0, 0,
				DIRECTION_ARROW_WIDTH, DIRECTION_ARROW_HEIGHT, this.directionRotater, false);
		c.drawBitmap(rotatedDirection, screenCoords.x - rotatedDirection.getWidth() / 2,
				screenCoords.y - rotatedDirection.getHeight() / 2, this.mPaint);
	}
}
 
Example #23
Source File: SimpleLocationOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(final Canvas c, final Projection pj) {
	if (this.mLocation != null) {
		pj.toPixels(this.mLocation, screenCoords);

		c.drawBitmap(PERSON_ICON, screenCoords.x - PERSON_HOTSPOT.x, screenCoords.y
				- PERSON_HOTSPOT.y, this.mPaint);
	}
}
 
Example #24
Source File: OsmPath.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Call this method at the beginning of every draw() call.
 */
public void onDrawCycle(Projection proj) {
	if (mLastZoomLevel != proj.getZoomLevel()) {
		proj.toPixels(sReferenceGeoPoint, mReferencePoint);
		mLastZoomLevel = proj.getZoomLevel();
	}
	int x = mReferencePoint.x;
	int y = mReferencePoint.y;
	proj.toPixels(sReferenceGeoPoint, mReferencePoint);
	int deltaX = mReferencePoint.x - x;
	int deltaY = mReferencePoint.y - y;

	offset(deltaX, deltaY);
}
 
Example #25
Source File: LinearRing.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * @since 6.2.0
 */
public void getBestOffset(final Projection pProjection, final PointL pOffset, final PointL pPixel) {
    final Rect screenRect = pProjection.getIntrinsicScreenRect();
    final double screenCenterX = (screenRect.left + screenRect.right) / 2.;
    final double screenCenterY = (screenRect.top + screenRect.bottom) / 2.;
    final double worldSize = pProjection.getWorldMapSize();
    getBestOffset(pPixel.x, pPixel.y, screenCenterX, screenCenterY, worldSize, pOffset);
}
 
Example #26
Source File: LinearRing.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Compute the pixel offset so that a list of pixel segments display in the best possible way:
 * the center of all pixels is as close to the screen center as possible
 * This notion of pixel offset only has a meaning on very low zoom level,
 * when a GeoPoint can be projected on different places on the screen.
 */
private void getBestOffset(final Projection pProjection, final PointL pOffset) {
	final double powerDifference = pProjection.getProjectedPowerDifference();
	final PointL center = pProjection.getLongPixelsFromProjected(
			mProjectedCenter, powerDifference, false, null);
	getBestOffset(pProjection, pOffset, center);
}
 
Example #27
Source File: SimpleFastPointOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Default action on tap is to select the nearest point.
 */
@Override
public boolean onSingleTapConfirmed(final MotionEvent event, final MapView mapView) {
    if(!mStyle.mClickable) return false;
    float hyp;
    Float minHyp = null;
    int closest = -1;
    Point tmp = new Point();
    Projection pj = mapView.getProjection();

    for(int i = 0; i < mPointList.size(); i++) {
        if(mPointList.get(i) == null) continue;
        // TODO avoid projecting coordinates, do a test before calling next line
        pj.toPixels(mPointList.get(i), tmp);
        if(Math.abs(event.getX() - tmp.x) > 50 || Math.abs(event.getY() - tmp.y) > 50) continue;
        hyp = (event.getX() - tmp.x) * (event.getX() - tmp.x)
                + (event.getY() - tmp.y) * (event.getY() - tmp.y);
        if(minHyp == null || hyp < minHyp) {
            minHyp = hyp;
            closest = i;
        }
    }
    if(minHyp == null) return false;
    setSelectedPoint(closest);
    mapView.invalidate();
    if(clickListener != null) clickListener.onClick(mPointList, closest);
    return true;
}
 
Example #28
Source File: LinearRing.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Dedicated to Polyline, as they can run much faster with drawLine than through a Path
 * @since 6.0.0
 */
void buildLinePortion(final Projection pProjection,
					  final boolean pStorePoints){
	final int size = mOriginalPoints.size();
	if (size < 2) { // nothing to paint
		return;
	}
       computeProjected();
       computeDistances();
	final PointL offset = new PointL();
	getBestOffset(pProjection, offset);
	mSegmentClipper.init();
	clipAndStore(pProjection, offset, mClosed, pStorePoints, mSegmentClipper);
	mSegmentClipper.end();
}
 
Example #29
Source File: PolyOverlayWithIW.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * @since 6.2.0
    * We pre-check if it's worth computing and drawing a poly.
    * How do we do that? As an approximation, we consider both the poly and the screen as disks.
    * Then, we compute the distance between both centers: if it's greater than the sum of the radii
    * the poly won't be visible.
 */
private boolean isVisible(final Projection pProjection) {
       // projecting the center and a corner of the bounding box to the screen, close to the screen center
	final BoundingBox boundingBox = getBounds();
       pProjection.toProjectedPixels(boundingBox.getCenterLatitude(), boundingBox.getCenterLongitude(),
               mVisibilityProjectedCenter);
       pProjection.toProjectedPixels(boundingBox.getLatNorth(), boundingBox.getLonEast(),
               mVisibilityProjectedCorner);
       pProjection.getLongPixelsFromProjected(mVisibilityProjectedCenter,
               pProjection.getProjectedPowerDifference(), true, mVisibilityRectangleCenter);
       pProjection.getLongPixelsFromProjected(mVisibilityProjectedCorner,
               pProjection.getProjectedPowerDifference(), true, mVisibilityRectangleCorner);

       // computing the distance and the radii
       final int screenCenterX = pProjection.getWidth() / 2;
       final int screenCenterY = pProjection.getHeight() / 2;
       final double radius = Math.sqrt(Distance.getSquaredDistanceToPoint(
               mVisibilityRectangleCenter.x, mVisibilityRectangleCenter.y,
               mVisibilityRectangleCorner.x, mVisibilityRectangleCorner.y));
       final double distanceBetweenCenters = Math.sqrt(Distance.getSquaredDistanceToPoint(
               mVisibilityRectangleCenter.x, mVisibilityRectangleCenter.y,
               screenCenterX, screenCenterY));
       final double screenRadius = Math.sqrt(Distance.getSquaredDistanceToPoint(
               0, 0,
               screenCenterX, screenCenterY));

       return distanceBetweenCenters <= radius + screenRadius;
}
 
Example #30
Source File: GroundOverlay2.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
protected void computeMatrix(final Projection pProjection) {
	long x0 = pProjection.getLongPixelXFromLongitude(mLonL),
			y0 = pProjection.getLongPixelYFromLatitude(mLatU),
			x1 = pProjection.getLongPixelXFromLongitude(mLonR),
			y1 = pProjection.getLongPixelYFromLatitude(mLatD);

	float widthOnTheMap = x1 - x0,
			heightOnTheMap = y1 - y0;

	float scaleX = widthOnTheMap / getImage().getWidth(),
			scaleY = heightOnTheMap / getImage().getHeight();

	getMatrix().setScale(scaleX, scaleY);
	getMatrix().postTranslate(x0, y0);
}