Java Code Examples for org.osmdroid.views.Projection#toPixels()

The following examples show how to use org.osmdroid.views.Projection#toPixels() . 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: 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 2
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 3
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 4
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 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: 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 7
Source File: OsmMarker.java    From FancyPlaces with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void draw(Canvas canvas, MapView mapView, boolean shadow) {
    if (this.Icon != null) {
        Projection pj = mapView.getProjection();
        Point PositionPixels = new Point();
        pj.toPixels(this.Position, PositionPixels);
        int width = this.Icon.getIntrinsicWidth();
        int height = this.Icon.getIntrinsicHeight();
        Rect rect = new Rect(0, 0, width, height);
        rect.offset(-((int) (this.AnchorU * (float) width)), -((int) (this.AnchorV * (float) height)));
        this.Icon.setBounds(rect);
        this.Icon.setAlpha((int) (this.Alpha * 255.0F));
        drawAt(canvas, this.Icon, PositionPixels.x, PositionPixels.y, false, 0);
    }
}
 
Example 8
Source File: ProjectionTest.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * "If both Projection's scrolls are 0, the geo center is projected to the screen rect center"
 */
@Test
public void testCenteredGeoPoint() {
    for (int zoomLevel = mMinZoomLevel; zoomLevel <= mMaxZoomLevel; zoomLevel ++) {
        final double mapSize = TileSystem.MapSize((double) zoomLevel);
        for (int i = 0; i < mNbIterations; i ++) {
            final GeoPoint geoPoint = getRandomGeoPoint();
            final Projection projection = getRandomProjection(zoomLevel, geoPoint, 0, 0);
            final Point pixel = projection.toPixels(geoPoint, null);

            int expectedX = mWidth / 2;
            if (mapSize < mWidth) { // side effect for low level, as the computed pixel will be the first from the left
                while (expectedX - mapSize >= 0) {
                    expectedX -= mapSize;
                }
            }
            Assert.assertEquals(expectedX, pixel.x);

            int expectedY = mHeight / 2;
            if (mapSize < mHeight) { // side effect for low level, as the computed pixel will be the first from the top
                while (expectedY - mapSize >= 0) {
                    expectedY -= mapSize;
                }
            }
            Assert.assertEquals(expectedY, pixel.y);
        }
    }
}
 
Example 9
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 10
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 11
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 12
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 13
Source File: ClickableIconOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * From {@link Marker#hitTest(MotionEvent, MapView)}
 * @return true, if this marker was taped.
 */
protected boolean hitTest(final MotionEvent event, final MapView mapView){
    final Projection pj = mapView.getProjection();

    // sometime at higher zoomlevels pj is null
    if ((mPosition == null) || (mPositionPixels == null) || (pj == null)) return false;

    pj.toPixels(mPosition, mPositionPixels);
    final Rect screenRect = pj.getIntrinsicScreenRect();
    int x = -mPositionPixels.x + screenRect.left + (int) event.getX();
    int y = -mPositionPixels.y + screenRect.top + (int) event.getY();
    boolean hit = mIcon.getBounds().contains(x, y);
    return hit;
}
 
Example 14
Source File: Marker.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override public void draw(Canvas canvas, Projection pj) {
	if (mIcon == null)
		return;
	if (!isEnabled())
		return;

	pj.toPixels(mPosition, mPositionPixels);

	float rotationOnScreen = (mFlat ? -mBearing : -pj.getOrientation()-mBearing);
	drawAt(canvas, mPositionPixels.x, mPositionPixels.y, rotationOnScreen);
	if (isInfoWindowShown()) {
		//showInfoWindow();
		mInfoWindow.draw();
	}
}
 
Example 15
Source File: ChatAttachAlertLocationLayout.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public void updatePositions() {
    if (mapView == null) {
        return;
    }
    Projection projection = mapView.getProjection();
    for (HashMap.Entry<Marker, View> entry : views.entrySet()) {
        Marker marker = entry.getKey();
        View view = entry.getValue();
        Point point = projection.toPixels(marker.getPosition(),null);
        view.setTranslationX(point.x - view.getMeasuredWidth() / 2);
        view.setTranslationY(point.y - view.getMeasuredHeight() + AndroidUtilities.dp(22));
    }
}
 
Example 16
Source File: LocationActivity.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public void updatePositions() {
    if (mapView == null) {
        return;
    }
    Projection projection = mapView.getProjection();
    for (HashMap.Entry<Marker, View> entry : views.entrySet()) {
        Marker marker = entry.getKey();
        View view = entry.getValue();
        Point point = projection.toPixels(marker.getPosition(),null);
        view.setTranslationX(point.x - view.getMeasuredWidth() / 2);
        view.setTranslationY(point.y - view.getMeasuredHeight() + AndroidUtilities.dp(22));
    }
}
 
Example 17
Source File: MyLocationNewOverlay.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
protected void drawMyLocation(final Canvas canvas, final Projection pj, final Location lastFix) {
	pj.toPixels(mGeoPoint, mDrawPixel);

	if (mDrawAccuracyEnabled) {
		final float radius = lastFix.getAccuracy()
				/ (float) TileSystem.GroundResolution(lastFix.getLatitude(),
						pj.getZoomLevel());

		mCirclePaint.setAlpha(50);
		mCirclePaint.setStyle(Style.FILL);
		canvas.drawCircle(mDrawPixel.x, mDrawPixel.y, radius, mCirclePaint);

		mCirclePaint.setAlpha(150);
		mCirclePaint.setStyle(Style.STROKE);
		canvas.drawCircle(mDrawPixel.x, mDrawPixel.y, radius, mCirclePaint);
	}

	if (lastFix.hasBearing()) {
		canvas.save();
		// Rotate the icon if we have a GPS fix, take into account if the map is already rotated
		float mapRotation;
		mapRotation=lastFix.getBearing();
		if (mapRotation >=360.0f)
			mapRotation=mapRotation-360f;
		canvas.rotate(mapRotation, mDrawPixel.x, mDrawPixel.y);
		// Draw the bitmap
		canvas.drawBitmap(mDirectionArrowBitmap, mDrawPixel.x
				- mDirectionArrowCenterX, mDrawPixel.y - mDirectionArrowCenterY,
				mPaint);
		canvas.restore();
	} else {
		canvas.save();
		// Unrotate the icon if the maps are rotated so the little man stays upright
		canvas.rotate(-mMapView.getMapOrientation(), mDrawPixel.x,
				mDrawPixel.y);
		// Draw the bitmap
		canvas.drawBitmap(mPersonBitmap, mDrawPixel.x - mPersonHotspot.x,
				mDrawPixel.y - mPersonHotspot.y, mPaint);
		canvas.restore();
	}
}
 
Example 18
Source File: SpeechBalloonOverlay.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public void draw(final Canvas pCanvas, final Projection pProjection) {
	final Paint background;
	final Paint foreground;
	if (mIsDragged) {
		background = mDragBackground != null ? mDragBackground : mBackground;
		foreground = mDragForeground != null ? mDragForeground : mForeground;
	} else {
		background = mBackground;
		foreground = mForeground;
	}
	if (mGeoPoint == null) {
		return;
	}
	if (mTitle == null || mTitle.trim().length() == 0) {
		return;
	}
	if (foreground == null || background == null) {
		return;
	}
	pProjection.toPixels(mGeoPoint, mPixel);
	final String text = mTitle;
	foreground.getTextBounds(text, 0, text.length(), mTextRect);
	mPoint.set(mPixel.x, mPixel.y);
	mTextRect.offset((int) (mPoint.x + mOffsetX + mDragDeltaX), (int) (mPoint.y + mOffsetY + mDragDeltaY));
	mTextRect.top -= mMargin;
	mTextRect.left -= mMargin;
	mTextRect.right += mMargin;
	mTextRect.bottom += mMargin;
	mRect.set(mTextRect.left, mTextRect.top, mTextRect.right, mTextRect.bottom);
	final int corner = mHelper.compute(mRect, mPoint, mRadius, mIntersection1, mIntersection2);
	pCanvas.drawRect(mTextRect.left, mTextRect.top, mTextRect.right, mTextRect.bottom, background);
	if (corner != SpeechBalloonHelper.CORNER_INSIDE) {
		mPath.reset();
		mPath.moveTo(mPoint.x, mPoint.y);
		mPath.lineTo(mIntersection1.x, mIntersection1.y);
		mPath.lineTo(mIntersection2.x, mIntersection2.y);
		mPath.close();
		pCanvas.drawPath(mPath, background);
	}
	pCanvas.drawText(text, mTextRect.left + mMargin, mTextRect.bottom - mMargin, foreground);
}
 
Example 19
Source File: LinearRing.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
/**
 * @since 6.0.3
 * Detection is done in screen coordinates.
 * @param tolerance in pixels
 * @return the first GeoPoint of the Polyline close enough to the point
 */
GeoPoint getCloseTo(final GeoPoint pPoint, final double tolerance,
					final Projection pProjection, final boolean pClosePath) {
	computeProjected();
	final Point pixel = pProjection.toPixels(pPoint, null);
	final PointL offset = new PointL();
	getBestOffset(pProjection, offset);
	clipAndStore(pProjection, offset, pClosePath, true, null);
	final double mapSize = pProjection.getWorldMapSize();
	final Rect screenRect = pProjection.getIntrinsicScreenRect();
	final int screenWidth = screenRect.width();
	final int screenHeight = screenRect.height();
	double startX = pixel.x; // in order to deal with world replication
	while(startX - mapSize >= 0) {
		startX -= mapSize;
	}
	double startY = pixel.y;
	while(startY - mapSize >= 0) {
		startY -= mapSize;
	}
	final double squaredTolerance = tolerance * tolerance;
	final PointL point0 = new PointL();
	final PointL point1 = new PointL();
	boolean first = true;
	int index = 0;
	for (final PointL point : mPointsForMilestones) {
		point1.set(point);
		if (first) {
			first = false;
		} else {
			for (double x = startX ; x < screenWidth ; x += mapSize) {
				for (double y = startY ; y < screenHeight ; y += mapSize) {
					final double projectionFactor = Distance.getProjectionFactorToSegment(x, y, point0.x, point0.y, point1.x, point1.y);
					final double squaredDistance = Distance.getSquaredDistanceToProjection(x, y, point0.x, point0.y, point1.x, point1.y, projectionFactor);
					if (squaredTolerance > squaredDistance) {
						final long pointAX = mProjectedPoints[2 * (index - 1)];
						final long pointAY = mProjectedPoints[2 * (index - 1) + 1];
						final long pointBX = mProjectedPoints[2 * index];
						final long pointBY = mProjectedPoints[2 * index + 1];
						final long projectionX = (long) (pointAX + (pointBX - pointAX) * projectionFactor);
						final long projectionY = (long) (pointAY + (pointBY - pointAY) * projectionFactor);
						return MapView.getTileSystem().getGeoFromMercator(
								projectionX, projectionY, Projection.mProjectedMapSize,
								null, false, false);
					}
				}
			}
		}
		point0.set(point1);
		index ++;
	}
	return null;
}
 
Example 20
Source File: Bug82WinDeath.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public void draw(Canvas canvas, Projection pProjection) {
    Log.i(TAG, "Drawing Bug82 Windeath circle");
    Point point = pProjection.toPixels(new GeoPoint(50.71838, -103.42443), new Point());
    canvas.drawCircle(point.x, point.y, 100.0f, innerPaint);
}