Android MapView overlay disappearing when zooming in

I'm working on a simple Android app for plotting routes on a map. All is going well, but I have an issue when zooming in on my Samsung Galaxy S2. It works fine on a Galaxy S3, so I'm wondering whether it's related to memory management on the lower specced device. It also works fine on the emulator.

Here is equivalent code located in the overlays onDraw method, just condensed for posting here:

Point current = new Point();
Path path = new Path();
Projection projection = mapView.getProjection();

Iterator<GeoPoint> iterator = pointList.iterator();
if (iterator.hasNext()) {
    projection.toPixels(iterator.next(), current);
    path.moveTo((float) current.x, (float) current.y);
} else return path;
while(iterator.hasNext()) {
    projection.toPixels(iterator.next(), current);
    path.lineTo((float) current.x, (float) current.y);

Paint roadPaint = new Paint();

canvas.drawPath(path, roadPaint);

It's not too dissimilar to most of the sample code floating around for doing this. I'm just wondering if anyone can confirm my suspicions and advise if there is anything I can do in terms of configuration or tweaks that I can do to force drawing at all zoom levels?

Thanks in advance.

Cheers, Nathan


The problem is that you are painting the overlay yourself for a very specific state of the mapview. You should use OverlayItem instead.

The OverlayItem is added to the MapView overlays collection, and the MapView handles all the re-drawing depending on it's own state ( zoom, location, etc )

public void draw( Canvas canvas, MapView mapView, boolean shadow )
    super.draw( canvas, mapView, shadow );

    int x1 = -1;
    int y1 = -1;
    int x2 = -1;
    int y2 = -1;

    Paint paint = new Paint();
    paint.setStyle( Paint.Style.STROKE );
    paint.setColor( GeoLocation.ROUTE_COLOR );
    paint.setStrokeWidth( STROKE_WIDTH );

    for ( int i = 0; i < mRouteGeoPoints.size(); i++ )
        Point point = new Point();
        mapView.getProjection().toPixels( geoPoints.get( i ), point );
        x2 = point.x;
        y2 = point.y;
        if ( i > 0 )
            canvas.drawLine( x1, y1, x2, y2, paint );
        x1 = x2;
        y1 = y2;

You said that code above was an equivalent (not the real code you are running) and that's clear because you are returning a Path object in a onDraw() which you couldn't.

The "compressed form" of code you show should work as well as using the drawLine(). So the problem should come from something else (may the original code).

Anyway, I'll give you a couple of hints:

  • When the top and bottom of object you are drawing to a canvas are both out of screen, the object is ignored and not drawn. Check if this is not whats happening with your path. See my answer in this post Android Map Overlay Disappears on Zoom
  • You don't need to rebuild the path object every time. You are probably already doing it, and that's why you made the short version above. See my answer in this post with some suggestions to improve path drawing: Overlay behavior when zooming
  • If for some reason you really want to use the slower approach of drawLine(), you can use the follwing to make the line look better:

    paint = new Paint();

Finally, if the issue remains, update your question with more relevant code and let me know. Maybe I can help further.


受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

  • 近距离缩放时折线消失(Polylines are disappearing when zoom closely)
    问题 我创建了七种不同的折线。 然而,当我仔细放大时,其中一些正在消失。 为什么会发生? 我怎样才能防止这种情况? 这是我的折线渲染器: func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline) renderer.strokeColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.9) renderer.lineWidth = 2.2 return renderer } //Thousands of parameters sending as a parameter func createPathWithPoints(_ points: [MKMapPoint]) { let arc = MKPolyline(points: points, count: points.count) mapView.addOverlay(arc) } 请帮忙! 回答1 我在使用 MKPolyline 时遇到了类似的问题,下面是我为解决这个问题所做的。 1) 确保在 viewDidLoad() 中有
  • 缩放时防止叠加消失 - MKMapView & MKOverlay(Preventing overlays from disappearing when zoomed - MKMapView & MKOverlay)
    问题 我正在开发一个 iPhone/iPad 应用程序,该应用程序通过 MKMapView 和 MKOverlay 使用半透明平铺地图叠加层。 我希望用户能够深入放大地图,但我的地图叠加层的分辨率只能达到 6 级。 我不想限制缩放。 但是......如果用户缩放“太远”然后滚动到一个新的图块,如果它们比覆盖图块树放大得更深,则不会绘制我的覆盖图。 我希望即使在用户放大时也能绘制叠加层。 是否有任何最佳实践可以实现这一目标? 我想出的两个选项: 1) 设置用户可以缩放多远的限制。 不理想,因为即使覆盖层是低分辨率的,它们也可能仍在透明度的着色范围内,并且透明覆盖层似乎消失是一种误导。 2)预渲染比其他方式更深的图块......这有效,但它使我的应用程序的大小增加了一个数量级。 必须有更好的方法。 有任何想法吗??? 更新:我能找到的最接近我正在做的示例代码是来自 WWDC 2010 的 Apple 的 TileMap 示例。他们的代码遇到同样的问题——如果你放大“太远”,地图叠加层不会显示。 回答1 我通过添加“OverZoom”模式修改了 Apple 的 TileMap 示例代码。 我已经发布了更多细节和我的代码作为这个问题的答案。 我希望我可以帮助其他任何遇到此问题的人。 回答2 MKTileOverlay 有一个 maximumZ 属性。 对我来说,我的瓦片服务器只渲染到缩放
  • Strange behaviour with Osmdroid overlay at high zoom levels
    I'm using the osmdroid 3.0.5 jar in my app to display a map view. I'm superimposing an overlay, consisting of horizontal and vertical lines. I've noticed that in certain locations only, at high zoom levels some of the lines disappear, then reappear as the map is dragged. I've produced a minimal sample app which demonstrates the problem which exhibits itself both on a real device and an emulator (Gingerbread 2.3.3). The complete code is shown below - (the 'transform' methods are necessary in the real app although they don't appear to be in this minimal sample) : public class DemoMap extends
  • Osmdroid 叠加在高缩放级别的奇怪行为(Strange behaviour with Osmdroid overlay at high zoom levels)
    问题 我在我的应用程序中使用 osmdroid 3.0.5 jar 来显示地图视图。 我正在叠加一个由水平线和垂直线组成的叠加层。 我注意到仅在某些位置,在高缩放级别下,某些线条会消失,然后在拖动地图时重新出现。 我制作了一个最小的示例应用程序,它演示了在真实设备和模拟器(Gingerbread 2.3.3)上都会出现的问题。 完整的代码如下所示 - (“转换”方法在实际应用程序中是必需的,尽管它们似乎不在这个最小示例中): public class DemoMap extends Activity implements MapViewConstants { private MapView mapView; private MapController mapController; private MapOverlay mmapOverlay = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.copymain); mapView = (MapView) findViewById(R.id.mapview); mapView.setTileSource
  • Polylines are disappearing when zoom closely
    I created seven different polylines. However some of them are disappearing when I zoom in closely. Why it is happening? How can I prevent this? Here is my polyline renderer: func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline) renderer.strokeColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.9) renderer.lineWidth = 2.2 return renderer } //Thousands of parameters sending as a parameter func createPathWithPoints(_ points: [MKMapPoint]) { let arc = MKPolyline(points: points, count: points
  • MapView 顶部的动画菜单按钮(Animated Menu Buttons on top of MapView)
    问题 我有一个 MapView,我想要一个动画的选项菜单,当用户单击顶部导航栏上的图标时,该菜单从屏幕左侧滑入并覆盖 MapView。 所需的行为是: 1. 用户单击顶部导航栏上的图标 2. 菜单从左侧滑入,显示具有不同选项的按钮。 3. 如果用户选择取消或返回Mapview,则菜单向右滑出并消失。 在 MapView 之上实现此行为的最佳方法是什么? 回答1 首先,您需要在您拥有地图视图的相同 XML 布局中添加按钮: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <view android:id="@+id/mv" android:layout_width="fill_parent" android:layout_height="fill_parent" class="com.google.android.maps.MapView" android:apiKey="your key" android:clickable="true" />
  • android maps circle overlay, dynamically change radius?
    I have a MapView in my app and am drawing a few circle overlays on this map. Everything is working fine, but when I zoom on the map, the overlay radius does not change. I have tried searching forums and google for a solution but could not find one that worked for me. Does anyone have any ideas? Here is my code: HelloGoogleMaps.java (main activity) package com.adam.maps; import java.util.Iterator; import java.util.List; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle
  • Android上Google地图的缩放事件(On zoom event for google maps on android)
    问题 我们正在构建一个使用google maps api for android的应用程序。 我有MapController和MapView,并使用以下命令启用了内置的缩放控件: mapView.setBuiltInZoomControls(true); 现在,当用户实际放大地图时,我想获得一个事件,该如何处理? 我找不到能检测到缩放级别变化的此类事件或任何常规事件。 更新 mapView.getZoomControls()已弃用。 并且文档建议使用mapView.setBuiltInZoomControls(bool)代替。 可以,但是我根本无法从内置的缩放控件中弄清楚如何处理事件。 回答1 借助Google Maps Android API v2,您可以像下面这样使用GoogleMap.OnCameraChangeListener: mMap.setOnCameraChangeListener(new OnCameraChangeListener() { private float currentZoom = -1; @Override public void onCameraChange(CameraPosition pos) { if (pos.zoom != currentZoom){ currentZoom = pos.zoom; // do you action
  • 在与缩放级别无关的 Android MapView 中绘制形状(Drawing shapes in Android MapView independent of the zoom level)
    问题 我需要使用 Android 的 MapView 绘制形状,就像您在 Google 地图上使用自定义地图创建的形状一样。 换句话说,如果我在地图上绘制一个形状,当我缩小它时,它应该缩小,无论缩放级别如何都覆盖地图的相同区域。 遵循 Android 参考的示例: @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { super.draw(canvas, mapView, shadow); //---translate the GeoPoint to screen pixels--- Point screenPts = new Point(); mapView.getProjection().toPixels(p, screenPts); Paint boxPaint = new Paint(); boxPaint.setColor(android.graphics.Color.WHITE); boxPaint.setStyle(Paint.Style.FILL); boxPaint.setAlpha(140); canvas.drawCircle(screenPts.x, screenPts.y, 20, boxPaint); return true;
  • 在 com.google.android.maps.MapView 中禁用平移/缩放(Disable pan/zoom in com.google.android.maps.MapView)
    问题 如何禁用 MapView 的平移/缩放功能(不是缩放控件,我想要一个完全静态的地图)? 我还注意到触摸地图似乎不会触发 MapView onClickListener,谁能详细说明原因? 回答1 在布局文件中使用android:clickable="false" 。 回答2 对于 Android 版 Google Maps API 的第 2 版,方法如下: map.getUiSettings().setScrollGesturesEnabled(false); 回答3 这是保证工作 mapView.getMap().getUiSettings().setAllGesturesEnabled(false); 回答4 请使用 Overlay 获取地图上的点击事件 http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/Overlay.html 回答5 我有同样的问题,以下解决方案是我能找到的最好的解决方案,并且满足了我的要求: mapView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if(event
  • 在Android中点击地图获取坐标(Get coordinates on tapping map in android)
    问题 我正在尝试进行以下操作:我有一个mapactivity,当用户点击地图时,它会显示该位置的坐标。 我已经覆盖了onclick方法,但它甚至没有被调用。 任何想法? public class MapPoint extends MapActivity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mappoints); MapView map = (MapView)findViewById(R.id.mapview); map.setOnClickListener(this); map.setBuiltInZoomControls(true); map.getController().setZoom(18); map.getController().setCenter(new GeoPoint(39735007, -8827330)); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false
  • 缩小时 OSMDroid 崩溃(OSMDroid crashes when zooming out)
    问题 我在我的应用程序中添加了一个 OSMDroid 地图,它工作正常,但如果我缩小,它就会崩溃。 我在用户当前位置加载地图时添加了一个项目,加载地图后在 AsyncTask 中再加载 10 个。 更新:工作解决方案 在我下面的旧代码中,我在用户位置的地图中添加了一个“基本” ItemizedItemOverlay ,并为地图创建了一个ItemizedItemOverlay 。 然后我加载了更多的 OverlayItems 并将它们添加到列表中,但它们没有出现在地图上。 现在我在 AsyncTask 完成加载所有项目后创建ItemizedItemOverlay ,现在所有项目都可见, OnItemSingleTapUp正在按预期工作,在地图上双击也能正常工作并且没有更多的崩溃。 我仍然不知道是什么导致了崩溃,但它是这样工作的。 可能的问题: OverlayItems 列表是用零个对象初始化的(显然),但是在我添加一个 OverlayItem 之后,它包含 1 个 OverlayItem + 11 个更多空对象(或空引用?空项目?正确的术语是什么?)编辑:没有引起问题,仍然很奇怪。 编辑:似乎我只有在添加更多项目时才会崩溃。 这是我的完整代码和 Logcat: mResourceProxy = new DefaultResourceProxyImpl
  • Trouble with overlay using MKPolyline & MKPolylineView
    I have added an overlay to my MKMapView using the addOverlay: method. The overlay was made using MKPolyline and stroked using MKPolylineView. The stroke color is blue, with an alpha value of 0.7. When the view initially loads, the overlay is drawn correctly but the surrounding areas of the overlay are blue as well... When I pinch & zoom out, the blue area is still there but it adjusts to my new zoom level. This is somewhat hard to describe... but basically I have a small rectangle of "normal map" trapped inside a larger rectangle that is blue. It will disappear when I zoom out to view the
  • Android ItemizedOverlay 问题(Android ItemizedOverlay<OverlayItem> issue)
    问题 我必须在地图上的特定点上放置一些叠加层,我从 API 获取点坐标。 我在异步任务中进行提取。 我成功地放置了叠加层,当我放大地图或跨越它时,响应非常缓慢。 这是我的 overlayitem 类和我的 asynctask public class Marker extends ItemizedOverlay<OverlayItem> { private Context con; private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); public Marker(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); // TODO Auto-generated constructor stub } public Marker(Drawable defaultMarker, Context context) { super(boundCenterBottom(defaultMarker)); con=context; // TODO Auto-generated constructor stub } @Override protected OverlayItem createItem(int i) { //
  • OSMDroid crashes when zooming out
    I´ve added a OSMDroid map in my app and it works fine, but if I zoom out, it crashes. I add one Item while loading the Map at the user´s current position, and load 10 more in an AsyncTask after loading the map. UPDATE: Working Solution In my old Code below, I added one "base"-Item to the map at the user´s location and created an ItemizedItemOverlay for the map. Then I loaded more OverlayItems and added them to the list, but they didn´t appear on the map. Now I create the ItemizedItemOverlay after the AsyncTask has finished loading all the Items, now the Items are all visible, OnItemSingleTapUp
  • OnTap() event on map is not fired
    Hello i am unable to fire ontap() event i want to add a marker whenever i tap on map, and when i tap on another geopoint the first marker should disappear and the marker should be added on new location... till now i have come to this point..can anybody tell me where am i going wrong!! thanks in advance Source Code public class GetLocation extends MapActivity implements OnClickListener { MapView mapView; MapController mc; GeoPoint p; int range; String category; Button view, traffic; private static final String Tag = "GetLocation class"; ZoomControls zoomControls; Canvas canvas; MapOverlay
  • 无论您在Android上的哪个位置放大地图,都可以保持地图居中(Keep map centered regardless of where you pinch zoom on android)
    问题 我正在寻找与Uber处理捏缩放事件类似的方式。 无论您在屏幕上的什么位置,地图都将居中并放大中心位置。 有没有办法在地图片段上没有某种覆盖的方法? 还是应该禁用地图事件,在地图片段上创建叠加层,并处理叠加层中的所有缩放/其他事件? 回答1 我花了大约3天的时间在Google上搜索后,才找到了完整的解决方案。 我的答案是从https://stackoverflow.com/a/32734436/3693334编辑的。 public class CustomMapView extends MapView { private int fingers = 0; private GoogleMap googleMap; private long lastZoomTime = 0; private float lastSpan = -1; private Handler handler = new Handler(); private ScaleGestureDetector scaleGestureDetector; private GestureDetector gestureDetector; public CustomMapView(Context context) { super(context); } public CustomMapView(Context context
  • Android Map Overlay Disappears on Zoom
    I'm having a headache with Android's Google Maps API... I draw some overlays on the map and it's all fine, it updates regularly and handles gestures well, no problem... However, when I zoom in to perhaps level 15-16 or beyond, one or more of the larger polygons aren't redrawn. This only occurs in cases where the centroid of a circle for example lies beyond the bounds of the screen, and only occurs when zooming in.. it does not occur when zooming out, nor does it occur if one pans the map in any direction (translation). When zooming in very close, I'm guessing the system disregards the overlay
  • Skobbler Annotation disappears from map when zooming out on Android
    Currently, I am adding a list of annotations to a mapview with code similar to the following: // Add to map view SKAnnotation annotation = new SKAnnotation(i++); annotation.getLocation().setLongitude(result.longitude); annotation.getLocation().setLatitude(result.latitude); annotation.setMininumZoomLevel(1); annotation.setAnnotationType(SKAnnotation.SK_ANNOTATION_TYPE_PURPLE); mapView.addAnnotation(annotation, SKAnimationSettings.ANIMATION_POP_OUT); Yet whenever I view the annotations on the map, they disappear after I zoom out to anything under zoom level 4.0. Looking at the docs for the
  • 如何在自定义 UIView 下方的 MKMapview 中启用 touchEvents(滚动和平移)?(How to enable touchEvents (scroll and pan) in MKMapview below a custom UIView?)
    问题 替代文字 http://www.gisnotes.com/wordpress/wp-content/uploads/2009/09/poly.png 简而言之,我试图弄清楚如何缩放在 MKMapView (mapView) 之上的自定义视图 (geometryView) 中实现的几何图形(点、线和多边形)。 我所做的是.. 创建 DrawMapViewController。 在底部工具栏上添加 UIBarButtonItems(MAP、PT、LN、PG)。 当您单击地图按钮时,您可以平移/缩放地图。 这通过设置 geometryView.hidden = YES 来启用 mapView; 当点击三个几何按钮中的任何一个时,geometryView.hidden = NO 显示几何视图,从而启用 touchEvents 并从 GeometryView.drawRect 的方法绘制几何。 图层顺序如下:mapView 位于geometryView 的底部。 -几何视图 -地图视图 我的问题是什么? 理想情况下,在“地图”模式下以及用户平移和缩放时,我希望是否可以从几何视图中显示绘图。 但是当用户点击“地图”时,geometryView.hidden = YES,因此绘图消失了。 如果我使几何视图可见,那么用户与几何视图而不是地图视图交互,因此没有缩放和平移。