google 地图 android 上的折线用于每次位置更改、新折线或设置点?

PolyLines on google maps android for every location change, new polyline or setPoints?

在我的应用程序中,当我四处跋涉时,我会为每个位置变化绘制一条折线。这可能是一次 8 小时的背包徒步旅行,所以我可能有数万个点可以绘制。

在我的测试中,我注意到当我放大到相当近时(比如说 17 或 18),即使在绘制了数千条线之后它也能很好地工作,但是当我缩小并且必须渲染地图时所有这些行,它变得缓慢,因为我的 phone 正在尝试处理所有内容。

我知道的另一种绘制多段线的方法是创建一个包含所有点 (LatLng) 的集合 (ArrayList),并将其传递给绘制单条线的 PolyLine 的 setPoints() 方法。这在事后回顾跋涉时显然效果很好,但我想 setPoints() 的内部必须为添加的每个新点遍历整个集合以绘制一条线,这最终可能是性能更差的原因,因为无论旧点是否可见(在视口内),它都必须这样做。

中间有什么东西吗? polyline.addPoint() 方法将是完美的,但我找不到类似的东西......现在作为一个权宜之计,我刚刚让我的多段线可见性可切换,这样我就可以在我时隐藏它们远距离缩小并需要四处移动地图,正如我所说,当我近距离放大时,这不是问题,因为它只需要渲染一个相当小的子集。

非常感谢ideas/suggestions。

TIA

这就是我想出的解决方案。它可能不是最优雅的,但我只是在我的车里开了 40 多英里,当我缩小时,我没有任何滞后。我还觉得,通过每 100 点(大约每 100 秒,因为我的位置侦听器每秒触发一次)只创建一条大线,我节省了每次位置更改时必须循环我的 latlng 数组的处理。现在我只需要在真正的跋涉中测试一下 :)

// create and add new poly line to map from previos location to new location
            PolylineOptions lineOptions = new PolylineOptions()
                    .add(new LatLng(previousLocation.getLatitude(), previousLocation.getLongitude()))
                    .add(new LatLng(location.getLatitude(), location.getLongitude()))
                    .color(Color.GREEN)
                    .width(5);
            // add the polyline to the map
            Polyline polyline = map.addPolyline(lineOptions);
            // set the zindex so that the poly line stays on top of my tile overlays
            polyline.setZIndex(1000);
            // add the poly line to the array so they can all be removed if necessary
            polylines.add(polyline);
            // add the latlng from this point to the array
            allLatLngs.add(currentLocationLatLng);

            // check if the positions added is a multiple of 100, if so, redraw all of the polylines as one line (this helps with rendering the map when there are thousands of points)
            if(allLatLngs.size() % 100 == 0) {
                // first remove all of the existing polylines
                for(Polyline pline : polylines) {
                    pline.remove();
                }
                // create one new large polyline
                Polyline routeSoFar = map.addPolyline(new PolylineOptions().color(Color.GREEN).width(5));
                // draw the polyline for the route so far
                routeSoFar.setPoints(allLatLngs);
                // set the zindex so that the poly line stays on top of my tile overlays
                routeSoFar.setZIndex(1000);
                // clear the polylines array
                polylines.clear();
                // add the new poly line as the first element in the polylines array
                polylines.add(routeSoFar);
            }