Java Code Examples for org.osmdroid.views.MapView#invalidate()

The following examples show how to use org.osmdroid.views.MapView#invalidate() . 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: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 7 votes vote down vote up
public static void addMarker(Context context, MapView map, Element element) {
    Marker marker = new Marker(map);
    marker.setPosition(new GeoPoint(element.lat, element.lon));
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    map.getOverlays().add(marker);
    map.invalidate();
    marker.setIcon(context.getResources().getDrawable(R.drawable.ic_location));

    marker.setTitle(String.valueOf(""));
    for (Map.Entry<String, String> tag : element.tags.entrySet()) {
        if (tag.getKey().equals("name")) {
            marker.setTitle(String.valueOf(tag.getValue()));
            break;
        }
    }
    if (marker.getTitle().equals(""))
        marker.setTitle(String.valueOf(element.id));

}
 
Example 2
Source File: SpeechBalloonOverlay.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onLongPress(final MotionEvent event, final MapView mapView) {
	boolean touched = hitTest(event, mapView);
	if (touched){
		if (mDraggable){
			//starts dragging mode:
			mIsDragged = true;
			mDragStartX = event.getX();
			mDragStartY = event.getY();
			mDragDeltaX = 0;
			mDragDeltaY = 0;
			mapView.invalidate();
		}
	}
	return touched;
}
 
Example 3
Source File: SpeechBalloonOverlay.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTouchEvent(final MotionEvent event, final MapView mapView) {
	if (mDraggable && mIsDragged) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			mDragDeltaX = event.getX() - mDragStartX;
			mDragDeltaY = event.getY() - mDragStartY;
			mOffsetX += mDragDeltaX;
			mOffsetY += mDragDeltaY;
			mDragDeltaX = 0;
			mDragDeltaY = 0;
			mIsDragged = false;
			mapView.invalidate();
			return true;
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			mDragDeltaX = event.getX() - mDragStartX;
			mDragDeltaY = event.getY() - mDragStartY;
			mapView.invalidate();
			return true;
		}
	}
	return false;
}
 
Example 4
Source File: SimpleFastPointOverlay.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event, final MapView mapView) {
    if(mStyle.mAlgorithm !=
            SimpleFastPointOverlayOptions.RenderingAlgorithm.MAXIMUM_OPTIMIZATION) return false;
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            startBoundingBox = mapView.getBoundingBox();
            startProjection = mapView.getProjection();
            break;

        case MotionEvent.ACTION_MOVE:
            hasMoved = true;
            break;

        case MotionEvent.ACTION_UP:
            hasMoved = false;
            startBoundingBox = mapView.getBoundingBox();
            startProjection = mapView.getProjection();
            mapView.invalidate();
            break;
    }
    return false;
}
 
Example 5
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 6
Source File: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
public static void addMarker(Context context, MapView map, double latitude, double longitude, String markerTitle) {
    Marker marker = new Marker(map);
    marker.setPosition(new GeoPoint(latitude, longitude));
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    map.getOverlays().add(marker);
    map.invalidate();
    marker.setIcon(context.getResources().getDrawable(R.drawable.ic_location));
    marker.setTitle(markerTitle);
}
 
Example 7
Source File: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
public static Marker addMarker(Context context, MapView map, double latitude, double longitude) {
    if (map == null || context == null) return null;
    Marker marker = new Marker(map);
    marker.setPosition(new GeoPoint(latitude, longitude));
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    marker.setIcon(context.getResources().getDrawable(R.drawable.ic_location));
    marker.setInfoWindow(null);
    map.getOverlays().add(marker);
    map.invalidate();
    return marker;
}
 
Example 8
Source File: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
public static Marker myLocation(Context context, MapView map, double latitude, double longitude) {
    if (map == null || context == null) return null;
    Marker marker = new Marker(map);
    marker.setPosition(new GeoPoint(latitude, longitude));
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    marker.setIcon(context.getResources().getDrawable(R.drawable.ic_gps_location));
    marker.setInfoWindow(null);
    map.getOverlays().add(marker);
    map.invalidate();
    return marker;
}
 
Example 9
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 10
Source File: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Add new {@link Marker} to {@link MapView}
 *
 * @param context  The context to use. Usually your {@link android.app.Application} or {@link android.app.Activity} object.
 * @param map      {@link MapView} to add marker on
 * @param position Location of the marker
 */
public static Marker addMarker(Context context, MapView map, GeoPoint position) {
    Marker marker = new Marker(map);
    marker.setPosition(position);
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    marker.setIcon(context.getResources().getDrawable(R.drawable.ic_location));
    marker.setTitle("Latitude: " + position.getLatitude() + "\n" + "Longitude: " + position.getLongitude());
    marker.setPanToView(true);
    marker.setDraggable(true);
    map.getOverlays().add(marker);
    map.invalidate();
    return marker;
}
 
Example 11
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 12
Source File: Marker.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
public void moveToEventPosition(final MotionEvent event, final MapView mapView){
    float offsetY = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, mDragOffsetY, mapView.getContext().getResources().getDisplayMetrics());
	final Projection pj = mapView.getProjection();
	setPosition((GeoPoint) pj.fromPixels((int)event.getX(), (int)(event.getY()-offsetY)));
	mapView.invalidate();
}
 
Example 13
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 14
Source File: IconOverlay.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
public IconOverlay moveTo(final IGeoPoint position, final MapView mapView){
    mPosition = position;
    mapView.invalidate();
    return this;
}
 
Example 15
Source File: Routing.java    From wikijourney_app with Apache License 2.0 4 votes vote down vote up
/**
 * Draws the WayPoints of the Road, with instructions on them
 * @param road The Road we are going to add the WayPoints to
 * @param map The MapView on which the WayPoints will be added
 */
public void drawRoadWithWaypoints(Road road, MapView map) {
    /* TODO add support for even more directions markers type */
    Drawable nodeIcon = ContextCompat.getDrawable(context, R.drawable.marker_node);
    for (int i = 0; i < road.mNodes.size(); i++) {
        RoadNode node = road.mNodes.get(i); // We get the i-ème node of the route
        Marker nodeMarker = new Marker(map);
        nodeMarker.setPosition(node.mLocation);
        nodeMarker.setIcon(nodeIcon);
        nodeMarker.setTitle(context.getString(R.string.step_nbr) + " " + i);
        map.getOverlays().add(nodeMarker);

        // And we fill the bubbles with the directions
        nodeMarker.setSnippet(node.mInstructions);
        nodeMarker.setSubDescription(Road.getLengthDurationText(node.mLength, node.mDuration));

        // Finally, we add an icon to the bubble
        Drawable icon = ContextCompat.getDrawable(context, R.drawable.ic_continue);
        switch (node.mManeuverType) {
            case 1:
                icon = ContextCompat.getDrawable(context, R.drawable.ic_continue);
                break;
            case 3:
                icon = ContextCompat.getDrawable(context, R.drawable.ic_slight_left);
                break;
            case 4:
                icon = ContextCompat.getDrawable(context, R.drawable.ic_turn_left);
                break;
            case 5:
                icon = ContextCompat.getDrawable(context, R.drawable.ic_sharp_left);
                break;
            case 6:
                icon = ContextCompat.getDrawable(context, R.drawable.ic_slight_right);
                break;
            case 7:
                icon = ContextCompat.getDrawable(context, R.drawable.ic_turn_right);
                break;
            case 8:
                icon = ContextCompat.getDrawable(context, R.drawable.ic_sharp_right);
                break;
            case 12:
                icon = ContextCompat.getDrawable(context, R.drawable.ic_u_turn);
                break;
            default:
                break;

        }
        nodeMarker.setImage(icon);
    }
    map.invalidate();
}
 
Example 16
Source File: Routing.java    From wikijourney_app with Apache License 2.0 2 votes vote down vote up
/**
 * Creates Polyline to bind nodes of the route, and draw it
 * @param route The Road calcuated between each GeoPoint
 * @param map The MapView to draw the Road on
 * @param context Needed to draw the Road, should be the Activity containing the MapView
 */
public void drawPolyline(Road route, MapView map, Context context) {
    Polyline roadOverlay = RoadManager.buildRoadOverlay(route, context);
    map.getOverlays().add(roadOverlay);
    map.invalidate();
}
 
Example 17
Source File: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Delete a marker from map
 *
 * @param marker {@link Marker} to be deleted
 * @param map    The {@link MapView} which holds the marker
 */
public static void deleteMarker(Marker marker, MapView map) {
    map.getOverlays().remove(marker);
    map.invalidate();
}