Java Code Examples for org.osmdroid.util.GeoPoint#getLongitude()

The following examples show how to use org.osmdroid.util.GeoPoint#getLongitude() . 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: ShapeConverter.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
private static GeoPoint fixOutOfRange(GeoPoint point){
    if (point.getLatitude()>90.00)
        point.setLatitude(90.00);
    else if (point.getLatitude()<-90.00)
        point.setLatitude(-90.00);

    if (abs(point.getLongitude())>180.00){
        double longitude = point.getLongitude();
        double diff = longitude > 0 ? -360 : 360;
        while (abs(longitude) > 180)
            longitude += diff;
        point.setLongitude(longitude);
    }

    return point;
}
 
Example 2
Source File: Rectangle.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Move the `Rectangle` to be centered on the given `latitude` and `longitude`, attempting to keep
 * the width and height (in meters) as equal as possible adjusting for changes in latitude.
 *
 * @param latitude the latitude of the new rectangle center
 * @param longitude the longitude of the new rectangle center.
 */
@Override
@SimpleFunction(description = "Moves the Rectangle so that it is centered on the given " +
    "latitude and longitude while attempting to maintain the width and height of the Rectangle " +
    "as measured from the center to the edges.")
public void SetCenter(double latitude, double longitude) {
  if (latitude < -90 || latitude > 90) {
    container.$form().dispatchErrorOccurredEvent(this, "SetCenter", ErrorMessages.ERROR_INVALID_POINT, latitude, longitude);
    return;
  }
  if (longitude < -180 || longitude > 180) {
    container.$form().dispatchErrorOccurredEvent(this, "SetCenter", ErrorMessages.ERROR_INVALID_POINT, latitude, longitude);
    return;
  }
  GeoPoint currentCenter = getCentroid();
  GeoPoint northPoint = new GeoPoint(north, currentCenter.getLongitude());
  GeoPoint southPoint = new GeoPoint(south, currentCenter.getLongitude());
  GeoPoint eastPoint = new GeoPoint(currentCenter.getLatitude(), east);
  GeoPoint westPoint = new GeoPoint(currentCenter.getLatitude(), west);
  double latExtent2 = GeometryUtil.distanceBetween(northPoint, southPoint) / 2.0;
  double longExtent2 = GeometryUtil.distanceBetween(eastPoint, westPoint) / 2.0;
  currentCenter.setCoords(latitude, longitude);
  north = currentCenter.destinationPoint(latExtent2, 0.0f).getLatitude();
  south = currentCenter.destinationPoint(latExtent2, 180.0f).getLatitude();
  east = currentCenter.destinationPoint(longExtent2, 90.0f).getLongitude();
  west = currentCenter.destinationPoint(longExtent2, 270.0f).getLongitude();
  clearGeometry();
  map.getController().updateFeaturePosition(this);
}
 
Example 3
Source File: Marker.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * sets the location on the planet where the icon is rendered
 * @param position
 */
public void setPosition(GeoPoint position){
	mPosition = position.clone();
	if (isInfoWindowShown()) {
		closeInfoWindow();
		showInfoWindow();
	}
	mBounds=new BoundingBox(position.getLatitude(),position.getLongitude(),position.getLatitude(),position.getLongitude());
}
 
Example 4
Source File: Polygon.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/** Build a list of GeoPoint as a rectangle. 
 * @param center of the rectangle
 * @param lengthInMeters on longitude
 * @param widthInMeters on latitude
 * @return the list of 4 GeoPoint
 */
public static ArrayList<IGeoPoint> pointsAsRect(GeoPoint center, double lengthInMeters, double widthInMeters){
	ArrayList<IGeoPoint> points = new ArrayList<IGeoPoint>(4);
	GeoPoint east = center.destinationPoint(lengthInMeters*0.5, 90.0f);
	GeoPoint south = center.destinationPoint(widthInMeters*0.5, 180.0f);
	double westLon = center.getLongitude()*2 - east.getLongitude();
	double northLat = center.getLatitude()*2 - south.getLatitude();
	points.add(new GeoPoint(south.getLatitude(), east.getLongitude()));
	points.add(new GeoPoint(south.getLatitude(), westLon));
	points.add(new GeoPoint(northLat, westLon));
	points.add(new GeoPoint(northLat, east.getLongitude()));
	return points;
}
 
Example 5
Source File: GroundOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
public void setPosition(final GeoPoint pTopLeft, final GeoPoint pTopRight,
                        final GeoPoint pBottomRight, final GeoPoint pBottomLeft) {
    mMatrix.reset();
    mTopLeft = new GeoPoint(pTopLeft);
    mTopRight = new GeoPoint(pTopRight);
    mBottomRight = new GeoPoint(pBottomRight);
    mBottomLeft = new GeoPoint(pBottomLeft);
    mBounds= new BoundingBox(pTopLeft.getLatitude(),pTopRight.getLongitude(),
        pBottomRight.getLatitude(),pTopLeft.getLongitude()
        );
}
 
Example 6
Source File: GroundOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
public void setPosition(final GeoPoint pTopLeft, final GeoPoint pBottomRight) {
    mMatrix.reset();
    mMatrixSrc = null;
    mMatrixDst = null;
    mTopLeft = new GeoPoint(pTopLeft);
    mTopRight = null;
    mBottomRight = new GeoPoint(pBottomRight);
    mBottomLeft = null;
    mBounds= new BoundingBox(pTopLeft.getLatitude(),pBottomRight.getLongitude(),
        pBottomRight.getLatitude(),pTopLeft.getLongitude()
    );
}
 
Example 7
Source File: GroundOverlay2.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * @param UL upper left
 * @param RD lower right
 */
public void setPosition(GeoPoint UL, GeoPoint RD)
   {
       mLatU = (float)UL.getLatitude();
       mLonL = (float)UL.getLongitude();
       mLatD = (float)RD.getLatitude();
       mLonR = (float)RD.getLongitude();
}
 
Example 8
Source File: LinearRing.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
protected void addGreatCircle(final GeoPoint startPoint, final GeoPoint endPoint, final int numberOfPoints) {
	//	adapted from page http://compastic.blogspot.co.uk/2011/07/how-to-draw-great-circle-on-map-in.html
	//	which was adapted from page http://maps.forum.nu/gm_flight_path.html

	// convert to radians
	final double lat1 = startPoint.getLatitude() * MathConstants.DEG2RAD;
	final double lon1 = startPoint.getLongitude() * MathConstants.DEG2RAD;
	final double lat2 = endPoint.getLatitude() * MathConstants.DEG2RAD;
	final double lon2 = endPoint.getLongitude() * MathConstants.DEG2RAD;

	final double d = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
			* Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
	/*
	double bearing = Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2),
			Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2))
			/ -MathConstants.DEG2RAD;
	bearing = bearing < 0 ? 360 + bearing : bearing;
	*/

	for (int i = 1; i <= numberOfPoints; i++) {
		final double f = 1.0 * i / (numberOfPoints + 1);
		final double A = Math.sin((1 - f) * d) / Math.sin(d);
		final double B = Math.sin(f * d) / Math.sin(d);
		final double x = A * Math.cos(lat1) * Math.cos(lon1) + B * Math.cos(lat2) * Math.cos(lon2);
		final double y = A * Math.cos(lat1) * Math.sin(lon1) + B * Math.cos(lat2) * Math.sin(lon2);
		final double z = A * Math.sin(lat1) + B * Math.sin(lat2);

		final double latN = Math.atan2(z, Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)));
		final double lonN = Math.atan2(y, x);
		GeoPoint p = new GeoPoint(latN * MathConstants.RAD2DEG, lonN * MathConstants.RAD2DEG);
		mOriginalPoints.add(p);
	}
}
 
Example 9
Source File: GeoPointInterpolator.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public GeoPoint interpolate(float fraction, GeoPoint a, GeoPoint b) {
    double lat = (b.getLatitude() - a.getLatitude()) * fraction + a.getLatitude();
    double lngDelta = b.getLongitude() - a.getLongitude();

    // Take the shortest path across the 180th meridian.
    if (Math.abs(lngDelta) > 180) {
        lngDelta -= Math.signum(lngDelta) * 360;
    }
    double lng = lngDelta * fraction + a.getLongitude();
    return new GeoPoint(lat, lng);
}
 
Example 10
Source File: IconPlottingOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLongPress(final MotionEvent e, final MapView mapView) {
    if (markerIcon != null) {
        GeoPoint pt = (GeoPoint) mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY(), null);
        /*
         * <b>Note</b></b: when plotting a point off the map, the conversion from
             * screen coordinates to map coordinates will return values that are invalid from a latitude,longitude
             * perspective. Sometimes this is a wanted behavior and sometimes it isn't. We are leaving it up to you,
             * the developer using osmdroid to decide on what is right for your application. See
             * <a href="https://github.com/osmdroid/osmdroid/pull/722">https://github.com/osmdroid/osmdroid/pull/722</a>
             * for more information and the discussion associated with this.
         */

        //just in case the point is off the map, let's fix the coordinates
        if (pt.getLongitude() < -180)
            pt.setLongitude(pt.getLongitude()+360);
        if (pt.getLongitude() > 180)
            pt.setLongitude(pt.getLongitude()-360);
        //latitude is a bit harder. see https://en.wikipedia.org/wiki/Mercator_projection
        if (pt.getLatitude() > mapView.getTileSystem().getMaxLatitude())
            pt.setLatitude(mapView.getTileSystem().getMaxLatitude());
        if (pt.getLatitude() < mapView.getTileSystem().getMinLatitude())
            pt.setLatitude(mapView.getTileSystem().getMinLatitude());

        Marker m = new Marker(mapView);
        m.setPosition(pt);
        m.setIcon(markerIcon);
        m.setImage(markerIcon);
        m.setTitle("A demo title");
        m.setSubDescription("A demo sub description\n" + pt.getLatitude() + "," + pt.getLongitude());
        m.setSnippet("a snippet of information");
        mapView.getOverlayManager().add(m);
        mapView.invalidate();
        return true;
    }
    return false;
}
 
Example 11
Source File: CirclePlottingOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLongPress(final MotionEvent e, final MapView mapView) {

    if (Configuration.getInstance().isDebugMapView()) {
        Log.d(IMapView.LOGTAG,"CirclePlottingOverlay onLongPress");
    }
    GeoPoint pt = (GeoPoint) mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY(), null);
        /*
         * <b>Note</b></b: when plotting a point off the map, the conversion from
             * screen coordinates to map coordinates will return values that are invalid from a latitude,longitude
             * perspective. Sometimes this is a wanted behavior and sometimes it isn't. We are leaving it up to you,
             * the developer using osmdroid to decide on what is right for your application. See
             * <a href="https://github.com/osmdroid/osmdroid/pull/722">https://github.com/osmdroid/osmdroid/pull/722</a>
             * for more information and the discussion associated with this.
         */

    //just in case the point is off the map, let's fix the coordinates
    if (pt.getLongitude() < -180)
        pt.setLongitude(pt.getLongitude() + 360);
    if (pt.getLongitude() > 180)
        pt.setLongitude(pt.getLongitude() - 360);
    //latitude is a bit harder. see https://en.wikipedia.org/wiki/Mercator_projection
    if (pt.getLatitude() > 85.05112877980659)
        pt.setLatitude(85.05112877980659);
    if (pt.getLatitude() < -85.05112877980659)
        pt.setLatitude(-85.05112877980659);

    List<GeoPoint> circle = Polygon.pointsAsCircle(pt, distanceKm);
    Polygon p = new Polygon(mapView);
    p.setPoints(circle);
    p.setTitle("A circle");
    mapView.getOverlayManager().add(p);
    mapView.invalidate();
    return true;

}
 
Example 12
Source File: GeometryUtil.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
public static Coordinate geoPointToCoordinate(GeoPoint p) {
  return new Coordinate(p.getLongitude(), p.getLatitude());
}
 
Example 13
Source File: LocationActivity.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private static GeoPoint move(GeoPoint startLL, double toNorth, double toEast) {
    double lonDiff = meterToLongitude(toEast, startLL.getLatitude());
    double latDiff = meterToLatitude(toNorth);
    return new GeoPoint(startLL.getLatitude() + latDiff, startLL.getLongitude() + lonDiff);
}
 
Example 14
Source File: LinearRing.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
/**
 * @since 6.0.3
 * Code comes from now gone method computeProjectedAndDistances
 */
private void computeProjected() {
	if (mProjectedPrecomputed) {
		return;
	}
	mProjectedPrecomputed = true;
	if (mProjectedPoints == null || mProjectedPoints.length != mOriginalPoints.size() * 2) {
		mProjectedPoints = new long[mOriginalPoints.size() * 2];
	}
	long minX = 0;
	long maxX = 0;
	long minY = 0;
	long maxY = 0;
	double north = 0;
	double east = 0;
	double south = 0;
	double west = 0;
	int index = 0;
	final PointL previous = new PointL();
	final PointL current = new PointL();
	final TileSystem tileSystem = MapView.getTileSystem();
	final double projectedMapSize = Projection.mProjectedMapSize;
	for (final GeoPoint currentGeo : mOriginalPoints) {
		final double latitude = currentGeo.getLatitude();
		final double longitude = currentGeo.getLongitude();
		tileSystem.getMercatorFromGeo(latitude, longitude, projectedMapSize, current, false);
		if (index == 0) {
			minX = maxX = current.x;
			minY = maxY = current.y;
			north = south = latitude;
			east = west = longitude;
		} else {
			setCloserPoint(previous, current, projectedMapSize);
			if (minX > current.x) {
				minX = current.x;
				west = longitude;
			}
			if (maxX < current.x) {
				maxX = current.x;
				east = longitude;
			}
			if (minY > current.y) {
				minY = current.y;
				north = latitude;
			}
			if (maxY < current.y) {
				maxY = current.y;
				south = latitude;
			}
		}
		mProjectedPoints[2 * index] = current.x;
		mProjectedPoints[2 * index + 1] = current.y;
		previous.set(current.x, current.y);
		index ++;
	}
	mProjectedWidth = maxX - minX;
	mProjectedHeight = maxY - minY;
	mProjectedCenter.set((minX + maxX) / 2, (minY + maxY) / 2);
	mBoundingBox.set(north, east, south, west);
}
 
Example 15
Source File: MilStdPointPlottingOverlay.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onLongPress(final MotionEvent e, final MapView mapView) {
    if (def != null) {
        GeoPoint pt = (GeoPoint) mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY(), null);

        //just in case the point is off the map, let's fix the coordinates
        if (pt.getLongitude() < -180)
            pt.setLongitude(pt.getLongitude() + 360);
        if (pt.getLongitude() > 180)
            pt.setLongitude(pt.getLongitude() - 360);
        //latitude is a bit harder. see https://en.wikipedia.org/wiki/Mercator_projection
        if (pt.getLatitude() > mapView.getTileSystem().getMaxLatitude())
            pt.setLatitude(mapView.getTileSystem().getMaxLatitude());
        if (pt.getLatitude() < mapView.getTileSystem().getMinLatitude())
            pt.setLatitude(mapView.getTileSystem().getMinLatitude());

        String code = def.getSymbolCode().replace("*", "-");
        //TODO if (!def.isMultiPoint())
        {
            int size = 128;

            SparseArray<String> attr = new SparseArray<>();
            attr.put(MilStdAttributes.PixelSize, size + "");

            ImageInfo ii = MilStdIconRenderer.getInstance().RenderIcon(code, def.getModifiers(), attr);
            Marker m = new Marker(mapView);
            m.setPosition(pt);
            m.setTitle(code);
            m.setSnippet(def.getDescription() + "\n" + def.getHierarchy());
            m.setSubDescription(def.getPath() + "\n" + m.getPosition().getLatitude() + "," + m.getPosition().getLongitude());

            if (ii != null && ii.getImage() != null) {
                BitmapDrawable d = new BitmapDrawable(ii.getImage());
                m.setImage(d);
                m.setIcon(d);

                int centerX = ii.getCenterPoint().x;    //pixel center position
                //calculate what percentage of the center this value is
                float realCenterX = (float) centerX / (float) ii.getImage().getWidth();

                int centerY = ii.getCenterPoint().y;
                float realCenterY = (float) centerY / (float) ii.getImage().getHeight();
                m.setAnchor(realCenterX, realCenterY);


                mapView.getOverlayManager().add(m);
                mapView.invalidate();
            }
        }

        return true;
    }
    return false;
}
 
Example 16
Source File: GeoPointInterpolator.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public GeoPoint interpolate(float fraction, GeoPoint a, GeoPoint b) {
    double lat = (b.getLatitude() - a.getLatitude()) * fraction + a.getLatitude();
    double lng = (b.getLongitude()- a.getLongitude()) * fraction + a.getLongitude();
    return new GeoPoint(lat, lng);
}