org.osmdroid.api.IGeoPoint Java Examples

The following examples show how to use org.osmdroid.api.IGeoPoint. 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: Googlev1WrapperSample.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * This is just used for debugging
 */
private void debugProjection() {
    new Thread() {
        @Override
        public void run() {
            // let the map get redrawn and a new projection calculated
            try {
                sleep(1000);
            } catch (InterruptedException ignore) {
            }

            // then get the projection
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    final IProjection projection = mMap.getProjection();
                    final IGeoPoint northEast = projection.getNorthEast();
                    final IGeoPoint southWest = projection.getSouthWest();
                    final IProjection breakpoint = projection;
                }
            });
        }
    }.start();
}
 
Example #2
Source File: Googlev2WrapperSample.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * This is just used for debugging
 */
private void debugProjection() {
    new Thread() {
        @Override
        public void run() {
            // let the map get redrawn and a new projection calculated
            try {
                sleep(1000);
            } catch (InterruptedException ignore) {
            }

            // then get the projection
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    final IProjection projection = mMap.getProjection();
                    final IGeoPoint northEast = projection.getNorthEast();
                    final IGeoPoint southWest = projection.getSouthWest();
                    final IProjection breakpoint = projection;
                }
            });
        }
    }.start();
}
 
Example #3
Source File: BoundBoxTest.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
@Test
public void testBoundingBoxMax() {

    List<IGeoPoint> partialPolyLine = new ArrayList<>();
    partialPolyLine.add(new GeoPoint(tileSystem.getMaxLatitude(),180d));
    partialPolyLine.add(new GeoPoint(tileSystem.getMinLatitude(),-180d));

    BoundingBox fromGeoPoints = BoundingBox.fromGeoPoints(partialPolyLine);
    Assert.assertEquals(fromGeoPoints.getCenterWithDateLine().getLatitude(),0d, 0.000001d);
    Assert.assertEquals(fromGeoPoints.getCenterWithDateLine().getLongitude(),0d, 0.000001d);


    Assert.assertEquals(fromGeoPoints.getLatNorth(),tileSystem.getMaxLatitude(), 0.000001d);
    Assert.assertEquals(fromGeoPoints.getLatSouth(),tileSystem.getMinLatitude(), 0.000001d);
    Assert.assertEquals(fromGeoPoints.getLonEast(),180d, 0.000001d);
    Assert.assertEquals(fromGeoPoints.getLonWest(),-180d, 0.000001d);
}
 
Example #4
Source File: BoundingBox.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
public static BoundingBox fromGeoPoints(final List<? extends IGeoPoint> partialPolyLine) {
	double minLat = Double.MAX_VALUE;
       double minLon = Double.MAX_VALUE;
       double maxLat = -Double.MAX_VALUE;
       double maxLon = -Double.MAX_VALUE;
	for (final IGeoPoint gp : partialPolyLine) {
		final double latitude = gp.getLatitude();
		final double longitude = gp.getLongitude();

		minLat = Math.min(minLat, latitude);
		minLon = Math.min(minLon, longitude);
		maxLat = Math.max(maxLat, latitude);
		maxLon = Math.max(maxLon, longitude);
	}

	return new BoundingBox(maxLat, maxLon, minLat, minLon);
}
 
Example #5
Source File: GeopackageSample.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
private void updateInfo() {
    StringBuilder sb = new StringBuilder();
    IGeoPoint mapCenter = mMapView.getMapCenter();
    sb.append(df.format(mapCenter.getLatitude()) + "," +
        df.format(mapCenter.getLongitude())
        + ",zoom=" + mMapView.getZoomLevelDouble());

    if (currentSource != null) {
        sb.append("\n");
        sb.append(currentSource.name() + "," + currentSource.getBaseUrl());
    }



    textViewCurrentLocation.setText(sb.toString());
}
 
Example #6
Source File: GeopackageFeatures.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
private void updateInfo() {
    StringBuilder sb = new StringBuilder();
    IGeoPoint mapCenter = mMapView.getMapCenter();
    sb.append(df.format(mapCenter.getLatitude()) + "," +
        df.format(mapCenter.getLongitude())
        + ",zoom=" + mMapView.getZoomLevelDouble());

    if (currentSource != null) {
        sb.append("\n");
        sb.append(currentSource.name() + "," + currentSource.getBaseUrl());
    }



    textViewCurrentLocation.setText(sb.toString());
}
 
Example #7
Source File: SphericalUtil.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the signed area of a closed path on a sphere of given radius.
 * The computed area uses the same units as the radius squared.
 * Used by SphericalUtilTest.
 */
static double computeSignedArea(List<IGeoPoint> path, double radius) {
    int size = path.size();
    if (size < 3) { return 0; }
    double total = 0;
    IGeoPoint prev = path.get(size - 1);
    double prevTanLat = tan((PI / 2 - toRadians(prev.getLatitude())) / 2);
    double prevLng = toRadians(prev.getLongitude());
    // For each edge, accumulate the signed area of the triangle formed by the North Pole
    // and that edge ("polar triangle").
    for (IGeoPoint point : path) {
        double tanLat = tan((PI / 2 - toRadians(point.getLatitude())) / 2);
        double lng = toRadians(point.getLongitude());
        total += polarTriangleArea(tanLat, lng, prevTanLat, prevLng);
        prevTanLat = tanLat;
        prevLng = lng;
    }
    return total * (radius * radius);
}
 
Example #8
Source File: Projection.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Adjust the offsets so that this geo point projects into that pixel
 * @since 6.0.0
 */
public void adjustOffsets(final IGeoPoint pGeoPoint, final PointF pPixel) {
	if (pPixel == null) {
		return;
	}
	if (pGeoPoint == null) {
		return;
	}
	final Point unRotatedExpectedPixel = unrotateAndScalePoint((int)pPixel.x, (int)pPixel.y, null);
	final Point unRotatedActualPixel = toPixels(pGeoPoint, null);
	final long deltaX = unRotatedExpectedPixel.x - unRotatedActualPixel.x;
	final long deltaY = unRotatedExpectedPixel.y - unRotatedActualPixel.y;
	adjustOffsets(deltaX, deltaY);
}
 
Example #9
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 #10
Source File: Bug164EndlessOnScolls.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
private void updateInfo(){
    IGeoPoint mapCenter = mMapView.getMapCenter();
    textViewCurrentLocation.setText(df.format(mapCenter.getLatitude())+","+
            df.format(mapCenter.getLongitude())
            +","+mMapView.getZoomLevelDouble() +"\nonScroll: " + callsScoll + " onZoom: "
            + callsZoom);

}
 
Example #11
Source File: DrawCircle10km.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
private void updateInfo() {
    IGeoPoint mapCenter = mMapView.getMapCenter();
    textViewCurrentLocation.setText(df.format(mapCenter.getLatitude()) + "," +
        df.format(mapCenter.getLongitude())
        + ",zoom=" + mMapView.getZoomLevelDouble() + ",angle=" + mMapView.getMapOrientation());

}
 
Example #12
Source File: GeometryUtil.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
public static double distanceBetween(IGeoPoint a, IGeoPoint b) {
  double lat1 = Math.toRadians(a.getLatitude());
  double lng1 = Math.toRadians(a.getLongitude());
  double lat2 = Math.toRadians(b.getLatitude());
  double lng2 = Math.toRadians(b.getLongitude());
  double dLat = lat2 - lat1;
  double dLng = lng2 - lng1;
  double cordlen = Math.pow(Math.sin(dLat / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dLng / 2), 2);
  double angle = 2 * Math.atan2(Math.sqrt(cordlen), Math.sqrt(1 - cordlen));
  return EARTH_RADIUS * angle;
}
 
Example #13
Source File: Projection.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public IGeoPoint fromPixels(final int x, final int y) {
	mPoint.x = x;
	mPoint.y = y;
	final LatLng latLng = mProjection.fromScreenLocation(mPoint);
	return new GeoPoint(latLng);
}
 
Example #14
Source File: MapController.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * @since 6.1.0
 */
@Override
public void animateTo(final IGeoPoint point, final Double pZoom, final Long pSpeed, final Float pOrientation, final Boolean pClockwise) {
    // If no layout, delay this call
    if (!mMapView.isLayoutOccurred()) {
        mReplayController.animateTo(point, pZoom, pSpeed, pOrientation, pClockwise);
        return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        final IGeoPoint currentCenter = new GeoPoint(mMapView.getProjection().getCurrentCenter());
        final MapAnimatorListener mapAnimatorListener =
                new MapAnimatorListener(this,
                        mMapView.getZoomLevelDouble(), pZoom,
                        currentCenter, point,
                        mMapView.getMapOrientation(), pOrientation, pClockwise);
        final ValueAnimator mapAnimator = ValueAnimator.ofFloat(0, 1);
        mapAnimator.addListener(mapAnimatorListener);
        mapAnimator.addUpdateListener(mapAnimatorListener);
        if (pSpeed == null) {
            mapAnimator.setDuration(Configuration.getInstance().getAnimationSpeedDefault());
        } else {
            mapAnimator.setDuration(pSpeed);
        }

        if (mCurrentAnimator != null) {
            mapAnimatorListener.onAnimationCancel(mCurrentAnimator);
        }
        mCurrentAnimator = mapAnimator;
        mapAnimator.start();
        return;
    }
    // TODO handle the zoom and orientation parts for the .3% of the population below HONEYCOMB (Feb. 2018)
    Point p = mMapView.getProjection().toPixels(point, null);
    animateTo(p.x, p.y);
}
 
Example #15
Source File: DrawPolygon.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
private void updateInfo() {
    IGeoPoint mapCenter = mMapView.getMapCenter();
    textViewCurrentLocation.setText(df.format(mapCenter.getLatitude()) + "," +
        df.format(mapCenter.getLongitude())
        + ",zoom=" + mMapView.getZoomLevelDouble() + ",angle=" + mMapView.getMapOrientation() + "\nBounds: " + mMapView.getBoundingBox().toString());

}
 
Example #16
Source File: Projection.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public Point toPixels(final IGeoPoint in, final Point out) {
	final LatLng latLng = new LatLng(in.getLatitude(), in.getLongitude());
	final Point point = mProjection.toScreenLocation(latLng);
	if (out != null) {
		out.x = point.x;
		out.y = point.y;
	}
	return point;
}
 
Example #17
Source File: GeopackageFeatureTiles.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
private void updateInfo() {
    StringBuilder sb = new StringBuilder();
    IGeoPoint mapCenter = mMapView.getMapCenter();
    sb.append(df.format(mapCenter.getLatitude()) + "," +
        df.format(mapCenter.getLongitude())
        + ",zoom=" + mMapView.getZoomLevelDouble());

    if (currentSource != null) {
        sb.append("\n");
        sb.append(currentSource.name() + "," + currentSource.getBaseUrl());
    }


    textViewCurrentLocation.setText(sb.toString());
}
 
Example #18
Source File: HeatMap.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * For each data point, find the corresponding cell, then increment the count. This is the
 * most inefficient portion of this example.
 * <p>
 * room for improvement: replace with some kind of geospatial indexing mechanism
 *
 * @param iGeoPoint
 * @param heatmap
 * @return
 */
private int increment(IGeoPoint iGeoPoint, Map<BoundingBox, Integer> heatmap) {

    Iterator<Map.Entry<BoundingBox, Integer>> iterator = heatmap.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<BoundingBox, Integer> next = iterator.next();
        if (next.getKey().contains(iGeoPoint)) {
            int newval = next.getValue() + 1;
            heatmap.put(next.getKey(), newval);
            return newval;
        }
    }
    return 0;
}
 
Example #19
Source File: GeometryUtil.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
public static YailList pointsListToYailList(List<? extends IGeoPoint> points) {
  List<YailList> entries = new ArrayList<YailList>();
  for (IGeoPoint point : points) {
    entries.add(asYailList(point));
  }
  return YailList.makeList(entries);
}
 
Example #20
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 rectangle defined as a BoundingBox
 * @return the list of 4 GeoPoint */
public static ArrayList<IGeoPoint> pointsAsRect(BoundingBox rectangle){
	ArrayList<IGeoPoint> points = new ArrayList<IGeoPoint>(4);
	points.add(new GeoPoint(rectangle.getLatNorth(), rectangle.getLonWest()));
	points.add(new GeoPoint(rectangle.getLatNorth(), rectangle.getLonEast()));
	points.add(new GeoPoint(rectangle.getLatSouth(), rectangle.getLonEast()));
	points.add(new GeoPoint(rectangle.getLatSouth(), rectangle.getLonWest()));
	return points;
}
 
Example #21
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 #22
Source File: ClickableIconOverlay.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/** used to recycle this */
public ClickableIconOverlay set(int id, IGeoPoint position, Drawable icon, DataType data) {
    set(position, icon);
    mId = id;
    mData = data;
    return this;
}
 
Example #23
Source File: NativeOpenStreetMapController.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLongPress(final MotionEvent pEvent, final MapView pMapView) {
  IGeoPoint p = pMapView.getProjection().fromPixels((int) pEvent.getX(), (int) pEvent.getY());
  final double lat = p.getLatitude();
  final double lng = p.getLongitude();
  for (MapEventListener l : eventListeners) {
    l.onLongPress(lat, lng);
  }
  return false;  // We don't want to cancel propagation to other overlays
}
 
Example #24
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 #25
Source File: SphericalUtil.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the location of origin when provided with a IGeoPoint destination,
 * meters travelled and original heading. Headings are expressed in degrees
 * clockwise from North. This function returns null when no solution is
 * available.
 * @param to       The destination IGeoPoint.
 * @param distance The distance travelled, in meters.
 * @param heading  The heading in degrees clockwise from north.
 */
public static IGeoPoint computeOffsetOrigin(IGeoPoint to, double distance, double heading) {
    heading = toRadians(heading);
    distance /= EARTH_RADIUS;
    // http://lists.maptools.org/pipermail/proj/2008-October/003939.html
    double n1 = cos(distance);
    double n2 = sin(distance) * cos(heading);
    double n3 = sin(distance) * sin(heading);
    double n4 = sin(toRadians(to.getLatitude()));
    // There are two solutions for b. b = n2 * n4 +/- sqrt(), one solution results
    // in the latitude outside the [-90, 90] range. We first try one solution and
    // back off to the other if we are outside that range.
    double n12 = n1 * n1;
    double discriminant = n2 * n2 * n12 + n12 * n12 - n12 * n4 * n4;
    if (discriminant < 0) {
        // No real solution which would make sense in IGeoPoint-space.
        return null;
    }
    double b = n2 * n4 + sqrt(discriminant);
    b /= n1 * n1 + n2 * n2;
    double a = (n4 - n2 * b) / n1;
    double fromLatRadians = atan2(a, b);
    if (fromLatRadians < -PI / 2 || fromLatRadians > PI / 2) {
        b = n2 * n4 - sqrt(discriminant);
        b /= n1 * n1 + n2 * n2;
        fromLatRadians = atan2(a, b);
    }
    if (fromLatRadians < -PI / 2 || fromLatRadians > PI / 2) {
        // No solution which would make sense in IGeoPoint-space.
        return null;
    }
    double fromLngRadians = toRadians(to.getLongitude()) -
        atan2(n3, n1 * cos(fromLatRadians) - n2 * sin(fromLatRadians));
    return new GeoPoint(toDegrees(fromLatRadians), toDegrees(fromLngRadians));
}
 
Example #26
Source File: SampleMapEventListener.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
private void updateInfo(){
    IGeoPoint mapCenter = mMapView.getMapCenter();
    textViewCurrentLocation.setText(df.format(mapCenter.getLatitude())+","+
            df.format(mapCenter.getLongitude())
            +",zoom="+mMapView.getZoomLevelDouble() + "\nBounds: " + mMapView.getBoundingBox().toString());

}
 
Example #27
Source File: GeoPoint.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * @see <a href="https://en.wikipedia.org/wiki/Haversine_formula">Haversine formula</a>
 * @see <a href="http://www.movable-type.co.uk/scripts/gis-faq-5.1.html">GIS FAQ</a>
 * @since 6.0.0
 * @return distance in meters
 */
public double distanceToAsDouble(final IGeoPoint other) {
	final double lat1 = DEG2RAD * getLatitude();
	final double lat2 = DEG2RAD * other.getLatitude();
	final double lon1 = DEG2RAD * getLongitude();
	final double lon2 = DEG2RAD * other.getLongitude();
	return RADIUS_EARTH_METERS * 2 * Math.asin(Math.min(1, Math.sqrt(
			Math.pow(Math.sin((lat2 - lat1) / 2), 2)
			+ Math.cos(lat1) * Math.cos(lat2)
			* Math.pow(Math.sin((lon2 - lon1) / 2), 2)
	)));
}
 
Example #28
Source File: GeoPoint.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * @see <a href="http://groups.google.com/group/osmdroid/browse_thread/thread/d22c4efeb9188fe9/bc7f9b3111158dd">discussion</a>
 * @return bearing in degrees
 */
public double bearingTo(final IGeoPoint other) {
	final double lat1 = Math.toRadians(this.mLatitude);
	final double long1 = Math.toRadians(this.mLongitude);
	final double lat2 = Math.toRadians(other.getLatitude());
	final double long2 = Math.toRadians(other.getLongitude());
	final double delta_long = long2 - long1;
	final double a = Math.sin(delta_long) * Math.cos(lat2);
	final double b = Math.cos(lat1) * Math.sin(lat2) -
					 Math.sin(lat1) * Math.cos(lat2) * Math.cos(delta_long);
	final double bearing = Math.toDegrees(Math.atan2(a, b));
	final double bearing_normalized = (bearing + 360) % 360;
	return bearing_normalized;
}
 
Example #29
Source File: StarterMapFragment.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //Note! we are programmatically construction the map view
    //be sure to handle application lifecycle correct (see note in on pause)
    mMapView = new MapView(inflater.getContext());
    mMapView.setDestroyMode(false);
    mMapView.setTag("mapView"); // needed for OpenStreetMapViewTest

    mMapView.setOnGenericMotionListener(new View.OnGenericMotionListener() {
        /**
         * mouse wheel zooming ftw
         * http://stackoverflow.com/questions/11024809/how-can-my-view-respond-to-a-mousewheel
         * @param v
         * @param event
         * @return
         */
        @Override
        public boolean onGenericMotion(View v, MotionEvent event) {
            if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_SCROLL:
                        if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f)
                            mMapView.getController().zoomOut();
                        else {
                            //this part just centers the map on the current mouse location before the zoom action occurs
                            IGeoPoint iGeoPoint = mMapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());
                            mMapView.getController().animateTo(iGeoPoint);
                            mMapView.getController().zoomIn();
                        }
                        return true;
                }
            }
            return false;
        }
    });
    return mMapView;
}
 
Example #30
Source File: OsmdroidShapeMarkers.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Polygon add a marker in the list of markers to where it is closest to the
 * the surrounding points
 *
 * @param marker
 * @param markers
 */
public static void addMarkerAsPolygon(Marker marker, List<Marker> markers) {
    IGeoPoint position = marker.getPosition();
    int insertLocation = markers.size();
    if (markers.size() > 2) {
        double[] distances = new double[markers.size()];
        insertLocation = 0;
        distances[0] = SphericalUtil.computeDistanceBetween(position,
                markers.get(0).getPosition());
        for (int i = 1; i < markers.size(); i++) {
            distances[i] = SphericalUtil.computeDistanceBetween(position,
                    markers.get(i).getPosition());
            if (distances[i] < distances[insertLocation]) {
                insertLocation = i;
            }
        }

        int beforeLocation = insertLocation > 0 ? insertLocation - 1
                : distances.length - 1;
        int afterLocation = insertLocation < distances.length - 1 ? insertLocation + 1
                : 0;

        if (distances[beforeLocation] > distances[afterLocation]) {
            insertLocation = afterLocation;
        }

    }
    markers.add(insertLocation, marker);
}