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

The following examples show how to use org.osmdroid.views.Projection#fromPixels() . 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: 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 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: 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 4
Source File: ScaleBarOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
private void drawLatitudeText(final Canvas canvas, final Projection projection) {
	// calculate dots per centimeter
	int xdpcm = (int) ((float) xdpi / 2.54);

	// get length in pixel
	int xLen = (int) (maxLength * xdpcm);

	// Two points, xLen apart, at scale bar screen location
	IGeoPoint p1 = projection.fromPixels((screenWidth / 2) - (xLen / 2), yOffset, null);
	IGeoPoint p2 = projection.fromPixels((screenWidth / 2) + (xLen / 2), yOffset, null);

	// get distance in meters between points
       final double xMeters = ((GeoPoint) p1).distanceToAsDouble(p2);
	// get adjusted distance, shortened to the next lower number starting with 1, 2 or 5
	final double xMetersAdjusted = this.adjustLength ? adjustScaleBarLength(xMeters) : xMeters;
	// get adjusted length in pixels
	final int xBarLengthPixels = (int) (xLen * xMetersAdjusted / xMeters);

	// create text
       final String xMsg = scaleBarLengthText(xMetersAdjusted);
	textPaint.getTextBounds(xMsg, 0, xMsg.length(), sTextBoundsRect);
	final int xTextSpacing = (int) (sTextBoundsRect.height() / 5.0);

	float x = xBarLengthPixels / 2 - sTextBoundsRect.width() / 2;
	if (alignRight)  x+= screenWidth -xBarLengthPixels;
	float y; 
	if (alignBottom) { y = screenHeight   -xTextSpacing*2; }
	else y = sTextBoundsRect.height() + xTextSpacing;
	canvas.drawText(xMsg, x, y, textPaint);
}
 
Example 5
Source File: ScaleBarOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
private void drawLongitudeText(final Canvas canvas, final Projection projection) {
	// calculate dots per centimeter
	int ydpcm = (int) ((float) ydpi / 2.54);

	// get length in pixel
	int yLen = (int) (maxLength * ydpcm);

	// Two points, yLen apart, at scale bar screen location
	IGeoPoint p1 = projection
			.fromPixels(screenWidth / 2, (screenHeight / 2) - (yLen / 2), null);
	IGeoPoint p2 = projection
			.fromPixels(screenWidth / 2, (screenHeight / 2) + (yLen / 2), null);

	// get distance in meters between points
       final double yMeters = ((GeoPoint) p1).distanceToAsDouble(p2);
	// get adjusted distance, shortened to the next lower number starting with 1, 2 or 5
	final double yMetersAdjusted = this.adjustLength ? adjustScaleBarLength(yMeters) : yMeters;
	// get adjusted length in pixels
	final int yBarLengthPixels = (int) (yLen * yMetersAdjusted / yMeters);

	// create text
       final String yMsg = scaleBarLengthText(yMetersAdjusted);
	textPaint.getTextBounds(yMsg, 0, yMsg.length(), sTextBoundsRect);
	final int yTextSpacing = (int) (sTextBoundsRect.height() / 5.0);

	float x; 
	if (alignRight)  {x = screenWidth  -yTextSpacing*2;}
	else x = sTextBoundsRect.height() + yTextSpacing;
	float y = yBarLengthPixels / 2 + sTextBoundsRect.width() / 2;
	if (alignBottom) y+= screenHeight -yBarLengthPixels;
	canvas.save();
	canvas.rotate(-90, x, y);
	canvas.drawText(yMsg, x, y, textPaint);
	canvas.restore();
}
 
Example 6
Source File: MilStdCustomPaintingSurface.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
private void touch_up() {
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    mPath.reset();
    if (map != null) {
        Projection projection = map.getProjection();


        if (symbol != null && symbol.getMinPoints() <= pts.size()) {


            ArrayList<GeoPoint> inputGeoPoints = new ArrayList<>();
            final Point unrotatedPoint = new Point();
            for (int i = 0; i < pts.size(); i++) {
                projection.unrotateAndScalePoint(pts.get(i).x, pts.get(i).y, unrotatedPoint);
                GeoPoint iGeoPoint = (GeoPoint) projection.fromPixels(unrotatedPoint.x, unrotatedPoint.y);
                inputGeoPoints.add(iGeoPoint);
            }

            MilStdMultipointOverlay overlay = new MilStdMultipointOverlay(symbol, inputGeoPoints);
            map.getOverlayManager().add(overlay);
            map.invalidate();
        }
    }

    pts.clear();

}
 
Example 7
Source File: FragmentiGapMap.java    From iGap-Android with GNU Affero General Public License v3.0 4 votes vote down vote up
private void drawMark(MotionEvent motionEvent, MapView mapView) {
    Projection projection = mapView.getProjection();
    GeoPoint loc = (GeoPoint) projection.fromPixels((int) motionEvent.getX(), (int) motionEvent.getY());
    OverlayItem mapItem = new OverlayItem("", "", new GeoPoint((((double) loc.getLatitudeE6()) / 1000000), (((double) loc.getLongitudeE6()) / 1000000)));
    drawMark(mapItem, false, 0);
}
 
Example 8
Source File: ScaleBarOverlay.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public void draw(Canvas c, Projection projection) {

	final double zoomLevel = projection.getZoomLevel();

	if (zoomLevel < minZoom) {
		return;
	}
	final Rect rect = projection.getIntrinsicScreenRect();
	int _screenWidth = rect.width();
	int _screenHeight = rect.height();
	boolean screenSizeChanged = _screenHeight!=screenHeight || _screenWidth != screenWidth;
	screenHeight = _screenHeight;
	screenWidth = _screenWidth;
	final IGeoPoint center = projection.fromPixels(screenWidth / 2, screenHeight / 2, null);
	if (zoomLevel != lastZoomLevel || center.getLatitude() != lastLatitude || screenSizeChanged) {
		lastZoomLevel = zoomLevel;
		lastLatitude = center.getLatitude();
		rebuildBarPath(projection);
	}

	int offsetX = xOffset;
	int offsetY = yOffset;
	if (alignBottom) offsetY *=-1;
	if (alignRight ) offsetX *=-1;
	if (centred && latitudeBar)
		offsetX += -latitudeBarRect.width() / 2;
	if (centred && longitudeBar)
		offsetY += -longitudeBarRect.height() / 2;

	projection.save(c, false, true);
	c.translate(offsetX, offsetY);

	if (latitudeBar && bgPaint != null)
		c.drawRect(latitudeBarRect, bgPaint);
	if (longitudeBar && bgPaint != null) {
		// Don't draw on top of latitude background...
		int offsetTop = latitudeBar ? latitudeBarRect.height() : 0;
		c.drawRect(longitudeBarRect.left, longitudeBarRect.top + offsetTop,
				longitudeBarRect.right, longitudeBarRect.bottom, bgPaint);
	}
	c.drawPath(barPath, barPaint);
	if (latitudeBar) {
		drawLatitudeText(c, projection);
	}
	if (longitudeBar) {
		drawLongitudeText(c, projection);
	}
	projection.restore(c, true);
}
 
Example 9
Source File: ScaleBarOverlay.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
protected void rebuildBarPath(final Projection projection) {   //** modified to protected
	// We want the scale bar to be as long as the closest round-number miles/kilometers
	// to 1-inch at the latitude at the current center of the screen.

	// calculate dots per centimeter
	int xdpcm = (int) ((float) xdpi / 2.54);
	int ydpcm = (int) ((float) ydpi / 2.54);

	// get length in pixel
	int xLen = (int) (maxLength * xdpcm);
	int yLen = (int) (maxLength * ydpcm);

	// Two points, xLen apart, at scale bar screen location
	IGeoPoint p1 = projection.fromPixels((screenWidth / 2) - (xLen / 2), yOffset, null);
	IGeoPoint p2 = projection.fromPixels((screenWidth / 2) + (xLen / 2), yOffset, null);

	// get distance in meters between points
       final double xMeters = ((GeoPoint) p1).distanceToAsDouble(p2);
	// get adjusted distance, shortened to the next lower number starting with 1, 2 or 5
	final double xMetersAdjusted = this.adjustLength ? adjustScaleBarLength(xMeters) : xMeters;
	// get adjusted length in pixels
	final int xBarLengthPixels = (int) (xLen * xMetersAdjusted / xMeters);

	// Two points, yLen apart, at scale bar screen location
	p1 = projection.fromPixels(screenWidth / 2, (screenHeight / 2) - (yLen / 2), null);
	p2 = projection.fromPixels(screenWidth / 2, (screenHeight / 2) + (yLen / 2), null);

	// get distance in meters between points
       final double yMeters = ((GeoPoint) p1).distanceToAsDouble(p2);
	// get adjusted distance, shortened to the next lower number starting with 1, 2 or 5
	final double yMetersAdjusted = this.adjustLength ? adjustScaleBarLength(yMeters) : yMeters;
	// get adjusted length in pixels
	final int yBarLengthPixels = (int) (yLen * yMetersAdjusted / yMeters);

	// create text
       final String xMsg = scaleBarLengthText(xMetersAdjusted);
	final Rect xTextRect = new Rect();
	textPaint.getTextBounds(xMsg, 0, xMsg.length(), xTextRect);
	int xTextSpacing = (int) (xTextRect.height() / 5.0);           

	// create text
       final String yMsg = scaleBarLengthText(yMetersAdjusted);
	final Rect yTextRect = new Rect();
	textPaint.getTextBounds(yMsg, 0, yMsg.length(), yTextRect);
	int yTextSpacing = (int) (yTextRect.height() / 5.0);
	int xTextHeight  = xTextRect.height();
	int yTextHeight  = yTextRect.height();

	barPath.rewind();
	
	//** alignBottom ad-ons
	int barOriginX = 0;
	int barOriginY = 0;
	int barToX     = xBarLengthPixels;
	int barToY	   = yBarLengthPixels;
	if (alignBottom) {
		xTextSpacing *= -1;
		xTextHeight  *= -1;
		barOriginY   = getMapHeight();
		barToY       = barOriginY -yBarLengthPixels;
	}
			
	if (alignRight) {
		yTextSpacing *= -1;
		yTextHeight  *= -1;		
		barOriginX   = getMapWidth();
		barToX       = barOriginX -xBarLengthPixels;    
	}
	
	if (latitudeBar) {
		// draw latitude bar
		barPath.moveTo(barToX, barOriginY +xTextHeight + xTextSpacing * 2);
		barPath.lineTo(barToX, barOriginY);
		barPath.lineTo(barOriginX, barOriginY);

		if (!longitudeBar) {
			barPath.lineTo(barOriginX,  barOriginY +xTextHeight + xTextSpacing * 2);
		}
		latitudeBarRect.set(barOriginX, barOriginY, barToX, barOriginY +xTextHeight + xTextSpacing * 2);
	}

	if (longitudeBar) {
		// draw longitude bar
		if (!latitudeBar) {
			barPath.moveTo(barOriginX +yTextHeight + yTextSpacing * 2, barOriginY);
			barPath.lineTo(barOriginX, barOriginY);
		}

		barPath.lineTo(barOriginX, barToY);
		barPath.lineTo(barOriginX +yTextHeight + yTextSpacing * 2, barToY);

		longitudeBarRect.set(barOriginX, barOriginY, barOriginX +yTextHeight + yTextSpacing * 2, barToY);
	}
}
 
Example 10
Source File: MapEventsOverlay.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView){
	Projection proj = mapView.getProjection();
	GeoPoint p = (GeoPoint)proj.fromPixels((int)e.getX(), (int)e.getY());
	return mReceiver.singleTapConfirmedHelper(p);
}
 
Example 11
Source File: MapEventsOverlay.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override public boolean onLongPress(MotionEvent e, MapView mapView) {
Projection proj = mapView.getProjection();
GeoPoint p = (GeoPoint)proj.fromPixels((int)e.getX(), (int)e.getY());
//throw event to the receiver:
return mReceiver.longPressHelper(p);
  }