简体   繁体   English

谷歌上的PolyLines映射android的每个位置更改,新的折线或setPoints?

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

In my app I'm drawing a polyline for every location change while I'm trekking around. 在我的应用程序中,我正在为每个位置变化绘制折线,而我正在徒步旅行。 This may be for an 8 hour backpacking hike so I may potentially have tens of thousands of points to plot. 这可能是一个8小时的背包徒步旅行,所以我可能有数万点的情节。

In my testing, I've noticed that when I'm zoomed in fairly close (say 17 or 18), it works great even after plotting thousands of lines, but as I zoom out and the map has to render ALL of those lines, it gets sluggish as my phone is trying to process everything. 在我的测试中,我注意到当我放大得很近(比如说17或18)时,即使在绘制了数千行之后也能很好地工作,但是当我缩小并且地图必须渲染所有这些行时,当我的手机试图处理一切时,它变得迟钝。

The other option that I know of to draw polylines is to create a collection (ArrayList) of all of my points (LatLng) and pass that in to the setPoints() method of the PolyLine which draws a single line. 我知道绘制折线的另一个选项是创建我所有点(LatLng)的集合(ArrayList)并将其传递给PolyLine的setPoints()方法,该方法绘制一条线。 This obviously works great when reviewing the trek after the fact, but I'd imagine the internals of setPoints() has to loop over the entire set for every new point that is added to draw that one line which ultimately is probably worse performance wise because it has to do that whether the older points are visible (within the viewport) or not. 这在事后审查trek时显然效果很好,但是我认为setPoints()的内部必须为每个新点循环整个集合,这些新点被添加到绘制一条线,最终可能是性能更差的因为无论旧点是否可见(在视口内)都必须这样做。

Is there something in between? 介于两者之间吗? A polyline.addPoint() method would be perfect, but I can't find anything like that... Right now as a stop-gap, I've just made my polylines visibility toggleable so that I can hide them when I zoom far out and need to move the map around, as I said, when I'm zoomed in closely, it's not a problem because it only has to render a fairly small subset. polyline.addPoint()方法将是完美的,但我找不到类似的东西......现在作为一个止损,我只是使我的折线可见性可切换,以便我可以隐藏它们当我缩放远如我所说,当我紧密地放大时,我需要移动地图,这不是问题,因为它只需渲染一个相当小的子集。

any ideas/suggestions are greatly appreciated. 任何想法/建议都非常感谢。

TIA TIA

This is what I came up with as a solution. 这就是我提出的解决方案。 It may not be the most elegant, but I just drove around for over 40 miles in my car and when I zoomed out, I had no lag whatsoever. 它可能不是最优雅的,但我只是开车四十多英里在我的车里,当我缩小时,我没有任何延迟。 I also feel that by only creating the one big line every 100 points (roughly every 100 seconds since my location listener only fires every second), I'm saving the processing of having to loop my latlng array for every location change. 我也觉得通过每100点创建一个大行(大约每隔100秒,因为我的位置监听器每秒都会触发),我正在保存必须为每个位置更改循环我的latlng数组的处理。 Now I just need to test this on a real trek :) 现在我只需要在真正的艰苦跋涉中测试这个:)

// 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);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM