【问题标题】:Drawing routes in Android collapses the device在 Android 中绘制路线会折叠设备
【发布时间】:2012-11-29 00:03:18
【问题描述】:

我正在开发一个 Android 应用程序,它必须在地图视图上显示一些“感兴趣的地方”以及设备当前位置。这很好用。 此外,在我的应用程序中,用户可以“点击”一个“感兴趣的地方”标记,应用程序必须绘制到该标记的路线。

我使用 Google Directions api 来获取路线,并使用折线解码器来获取用户和地点之间的 GeoPoints。对于我的测试路线,谷歌给了我大约 200 个不同的 GeoPoints。

所以,我有一个这样的类来添加这些 GeoPoints:

public class RouteOverlay extends Overlay {
    private GeoPoint gp1;
    private GeoPoint gp2;
    private int color;

    public RouteOverlay(GeoPoint gp1, GeoPoint gp2, int color) {
        this.gp1 = gp1;
        this.gp2 = gp2;
        this.color = color;
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        Projection projection = mapView.getProjection();
        Paint paint = new Paint();
        Point point = new Point();
        projection.toPixels(gp1, point);
        paint.setColor(color);
        Point point2 = new Point();
        projection.toPixels(gp2, point2);
        paint.setStrokeWidth(5);
        paint.setAlpha(120);
        canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
        super.draw(canvas, mapView, shadow);
    }

}

我绘制路线的方法如下: 1)检测onClick事件到地图中的标记。 2) 从那个事件中,我创建了一个新线程,在那里我调用了 Google API。 3) 一旦我得到结果,我在 GeoPoint 列表中解析/转换它。 4)然后我调用我的drawPath方法:

private void drawPath(List<GeoPoint> geoPoints, int color) {    
    mapOverlays.clear();
    mapOverlays.add(myLocationOverlay);
    mapOverlays.add(itemizedoverlay);
    for (int i = 1; i < geoPoints.size(); i++) {            
    mapOverlays.add(new RouteOverlay(geoPoints.get(i - 1), geoPoints.get(i), color));
    }
    mapView.postInvalidate();

5) 最后,我回到 UI 线程。

此方法清除地图覆盖列表(mapOverlays)。然后,将当前位置和“感兴趣的地方”叠加到列表中。最后,添加路线覆盖。

问题是,突然之间,工作非常缓慢,最后崩溃了。但是 LogCat 中没有消息。所以,我认为 30 个叠加层 + 1 个 + 超过 200 个的路线对于手机来说太多了。但是我看到的教程是这样做的......

如果我做错了什么,谁能告诉我? 提前致谢。

【问题讨论】:

    标签: android android-mapview


    【解决方案1】:

    我发现我做错了什么。

    当我调用drawPath函数时,在得到GeoPoints列表后,我做了一个循环来检查每个点的坐标。类似的东西

    for (int i = 0; i < geoList.size(); i++){
        Log.i("GEOPOINT " + i, geoList.get(i).toString());
        drawpath(geoList, Color.BLUE);
    }
    

    drawPath 函数被调用了 N 次。所以设备崩溃了。我的错。 凌晨2:00编程对代码不好!

    【讨论】:

      猜你喜欢
      • 2015-08-12
      • 2016-05-26
      • 1970-01-01
      • 2014-03-19
      • 2016-04-12
      • 1970-01-01
      • 2017-02-13
      • 2016-01-10
      • 2015-03-17
      相关资源
      最近更新 更多