简体   繁体   English

如何使用PolylineOptions从Google Map Android V2清除路线并再次绘制一个新路线?

[英]How to clear route from google map android v2 using PolylineOptions and draw a new one again?

I am getting the latitude and longitude on every onLocationChange and make a route like this: 我在每个onLocationChange上获取经度和纬度,并进行如下路由:

public void onLocationChanged(Location location) {


      PolylineOptions rectLine2;
      rectLine2.add(new LatLng(lat, lon));
      mGoogleMap.addPolyline(rectLine2);
}

Now I want to clear this route on one click, and again when I move, a fresh route should draw, but in my case, old route doesn't clear. 现在,我想一键清除此路线,然后再次移动时,应该绘制一条新路线,但就我而言,旧路线无法清除。

I did the following but not working: 我做了以下工作,但没有工作:

mGoogleMap.clear();// It clear's the map, but when I again draw the old route again comes.

Also

Polyline polyline;
polyline = mGoogleMap.addPolyline(rectLine2);
polyline.reomve()// This also didn't work.

Is there any other solution to clear the map? 还有其他解决方案可以清除地图吗?

Thanks In Advance! 提前致谢!

mGoogleMap.clear()

is used to clear the whole map so that you can redraw the polylines... 用于清除整个地图,以便您可以重画折线...

else you need to assign a variable to polyline and you can remove it... 否则,您需要为多义线分配一个变量,然后可以将其删除...

PolylineOptions rectLine2;
rectLine2.add(new LatLng(lat, lon));
Polyline polyline = mGoogleMap.addPolyline(rectLine2);
polyline.remove();

These are the ways... Why do you need any other solution to clear..? 这些是方法...为什么您需要其他解决方案来清除..?

If you have number of polylines then just create an array and remove all polylines once you need to clear 如果折线数量很多,则只需创建一个数组并在需要清除后删除所有折线即可

Hmm, that's intresting, try this way, it's working for me: 嗯,这很有趣,尝试这种方式,对我有用:

private void clearPath() {
    if (polyline != null)
        polyline.remove();
    polyline = null;
}

Try clearing your rectLine2 variable also. 也尝试清除rectLine2变量。 May be it's having all old latlangs. 可能是所有旧的latlang。 So on your click. 因此,单击即可。

rectLine2 = new PolylineOptions();

mGoogleMap.clear() is the best way to clean the map. mGoogleMap.clear()是清洁地图的最佳方法。 It removes all markers, polylines, polygons, overlays, etc from the map. 它将从地图上删除所有标记,折线,多边形,叠加层等。 see this http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#clear() 看到这个http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#clear()

So the problem is not in that. 因此问题不在于此。 You are doing something wrong in reDrawing the new path. 您在重新绘制新路径时做错了。 I think you have not given all code in your onLocationChanged function. 我认为您没有在onLocationChanged函数中提供所有代码。 And if this is your final code than this should not work as you are only adding one point in the PolylineOptions. 如果这是您的最终代码,那么这将不起作用,因为您仅在PolylineOptions中添加了一点。 Please check this How to draw interactive Polyline on route google maps v2 android 请检查此如何在路线谷歌地图v2 android上绘制交互式折线

Hope it will help 希望对你有帮助

Use map.clear() method to clear all map (polylines, markers, etc) or save all polylines somewhere and them remove them to save everything else 使用map.clear()方法清除所有地图(折线,标记等)或将所有折线保存在某处,然后将其删除以保存其他所有内容

                    for(Polyline polyline : polylineList) {
                    polyline.remove();
                }

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

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