我想知道,最好的做法是借鉴与谷歌地图API第2版的地图上的动态路由。我想有一个地图,是能够延长路线当用户正在移动。似乎有利用折线和PolylineOptions显而易见的解决方案。但我无法找到一个简单的方法加点 我实例化的折线后。要绘制折线是这样的:
I'm wondering what the best practice is to draw a dynamic route on a map with the Google Maps API v2. I want to have a map that's able to prolong the route while the user is moving. There seems to be the obvious solution by using a Polyline and PolylineOptions. But I just can't find an easy way to add points after I instantiated the Polyline. To draw a Polyline is something like this:
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.add(POINT1, POINT2, POINT3);
Polyline line = googleMap.addPolyline(polylineOptions);
但经过我通过该行的GoogleMap我不能添加任何新的指向它。类似于
But after I passed the line to GoogleMap I can't add any new points to it. Something like
polylineOptions.add(POINT1, POINT2, POINT3);
没有任何东西到我的路由添加。
doesn't add anything to my route.
我可以只添加完整的新的折线。但是没有办法,只是延长了现有的?我想出了一个办法,通过获取折线的所有点,添加新的点,并将它们写入回线:
I could just add complete new Polyline. But isn't there a way to prolong just the existing one? I figured out a way by getting all the points of the Polyline, add the new point, and write them back to the line:
List<LatLng> points = line.getPoints();
points.add(POINT4);
line.setPoints(points);
不过,这似乎是麻烦我。任何想法?
But it seems to be cumbersome to me. Any ideas?
在 mainActivity
类,定义了一个私有静态经纬度
变量名为 $ P $光伏
并初始化为(0,0)
第一。另外,还要一个标志变量,并为其分配0给它。在听者的 OnLocationChanged
方法,创建一个局部变量经纬度
的电流
并获得当前坐标这里...先检查标志的值,如果是0,则分配电流
到 preV
。然后添加一个折线。
In the mainActivity
class, define a private static LatLng
variable named prev
and initialize it to (0,0)
first. Also make a flag variable and assign 0 to it. In the Listener's OnLocationChanged
method, create a local variable LatLng
named current
and get current co-ordinates here... check the value of flag first, if it is 0 then assign current
to prev
. Then add a polyline.
指定电流
到 $ P $光伏
再次(这将每一次发生的第一时间后,标志为1)
Assign current
to prev
again (this will happen every time as after the first time, the flag will be 1)
例如:
public void onLocationChanged(Location location)
{
LatLng current = new LatLng(location.getLatitude(), location.getLongitude());
if(flag==0) //when the first update comes, we have no previous points,hence this
{
prev=current;
flag=1;
}
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(current, 16);
map.animateCamera(update);
map.addPolyline((new PolylineOptions())
.add(prev, current).width(6).color(Color.BLUE)
.visible(true));
prev=current;
current = null;
}
这样的事情。当然,性能的提升是可以,这只是一个例子code。但它应该工作。从而每次折线将只添加了previous和当前点,延长它逐点。
Something like this. Of course performance improvements can be made, this is just an example code. But it should work. Every time the polyline will add only the previous and current point, thereby extending it point by point.