【问题标题】:Android google maps getting rid of polylineAndroid 谷歌地图摆脱折线
【发布时间】:2015-04-25 03:03:15
【问题描述】:

我正在构建一个获取用户当前位置并查找附近景点的 android 应用程序。当我选择一个景点时,会从当前位置绘制一条路线,但是当我第二次这样做时,第一条路线停留在那里,我希望它消失。下面是我用来画线的代码。每次绘制一个方向时都会调用它。每次调用方法之前,我都尝试使用line.remove,但这会删除两行。有什么建议吗?

for (int i = 0; i < pontos.size() - 1; i++) {
                    LatLng src = pontos.get(i);
                    LatLng dest = pontos.get(i + 1);
                    try{
                        //here is where it will draw the polyline in your map
                        line = mMap.addPolyline(new PolylineOptions()
                                .add(new LatLng(src.latitude, src.longitude),
                                        new LatLng(dest.latitude,                dest.longitude))
                                .width(2).color(Color.RED).geodesic(true));

【问题讨论】:

    标签: android google-maps-android-api-2 polyline


    【解决方案1】:

    将您的Polylines 保存在一个数组中,以便在添加其他人之前将其删除:

    List<Polyline> mPolylines = new ArrayList<>();
    
    private void someMethod() {
        // Remove polylines from map
        for (Polyline polyline : mPolylines) {
            polyline.remove();
        }
        // Clear polyline array
        mPolylines.clear();
    
        for (int i = 0; i < pontos.size() - 1; i++) {
            LatLng src = pontos.get(i);
            LatLng dest = pontos.get(i + 1);
    
            mPolylines.add(mMap.addPolyline(new PolylineOptions()
                    .add(new LatLng(src.latitude, src.longitude),
                            new LatLng(dest.latitude, dest.longitude))
                    .width(2).color(Color.RED).geodesic(true)));
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多