Bla*_*mes 6 android google-maps polyline
在我的应用程序中,我正在为我徒步旅行时的每个位置变化绘制一条折线。这可能是一次 8 小时的背包徒步旅行,所以我可能有数万个点要绘制。
在我的测试中,我注意到当我放大得相当近时(比如 17 或 18),即使在绘制了数千条线之后它也能很好地工作,但是当我缩小并且地图必须渲染所有这些线时,当我的手机试图处理所有事情时,它变得缓慢。
我知道绘制折线的另一个选项是创建我所有点 (LatLng) 的集合 (ArrayList) 并将其传递给 PolyLine 的 setPoints() 方法,该方法绘制一条线。这在事后回顾跋涉时显然很有效,但我想 setPoints() 的内部结构必须为每个添加的新点循环整个集合以绘制那条线,这最终可能会导致性能更差,因为无论旧点是否可见(在视口内),它都必须这样做。
中间有什么吗?polyline.addPoint() 方法将是完美的,但我找不到类似的东西......现在作为一个权宜之计,我刚刚使我的折线可见性可切换,以便我可以在放大时隐藏它们需要移动地图,正如我所说,当我放大时,这不是问题,因为它只需要渲染一个相当小的子集。
非常感谢任何想法/建议。
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3485 次 |
| 最近记录: |