随着用户移动,在Android谷歌地图中绘制一条折线 [英] Draw a polyline in Android google maps as the user move

查看:181
本文介绍了随着用户移动,在Android谷歌地图中绘制一条折线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,而且我已经了解了android Google地图.我想实时跟踪用户移动并在android谷歌地图中绘制折线路径,有人可以帮我提供示例吗?我可以获取位置变更间隔,但仍然不知道如何将其应用于折线并将数据LatLng保留在数组中.

Hy, I'm a newbie in Android, and I've learned about android google maps. I'd like to tracking user movement and draw polyline path in android google maps in real time, can someone help me with the example? I can get the location chage interval but still don't know how to apply it into polyline and keep the data LatLng into array.

推荐答案

您需要在gradle中添加此/相关的g.play服务病毒码,以防万一.

You need to add this/related g.play services virsion line in your gradle in case you haven't.

compile 'com.google.android.gms:play-services-maps:8.4.0'

作为官方文档说,使用此代码.

GoogleMap map;
 // ... get a map.
 // Add a thin red line from London to New York.
 Polyline line = map.addPolyline(new PolylineOptions()
     .add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
     .width(5)
     .color(Color.RED));

注意:必须在主线程上调用修改Polyline的方法,否则,将在运行时引发 IllegalStateException .

Note: Methods that modify a Polyline must be called on the main thread If not, an IllegalStateException will be thrown at run-time.

为了我知道你可以找到此代码,所以这是我的小逻辑

For a sake that i know you can find this code so here is my small logic

  1. 保留开始和结束位置Latlng变量(startLatlngendLatlng)
  2. 在示例中您可以看到,您无法发送硬编码值,不能传递真实值,因此请在用户移动时或可能在特定时间段后获取位置
  3. 写逻辑:startLatlng =在一段时间后用户移动/时获得的新Latlng数据;
  4. 第一次
  5. startLatlng = endLatlng(如果条件为 boolian 并在调用后更改其值,请使用)
  6. 调用您的方法以(startLatlngendLatlng)绘制多段线
  7. endLatlng = startLatlng(仅在调用该draw方法将起始值设置为结束值后可见)
  1. Keep start and end position Latlng variables (startLatlng ,endLatlng)
  2. As you can see in the example you cannot send hard coded values,pass your real values so take the location when user moves or may be after a certain time period
  3. Write logic : startLatlng = new Latlng data that you got when user move / after a time period;
  4. only for the first time startLatlng = endLatlng (use if condition with a boolian and change its value after it gets called )
  5. call your method to draw Poly-line line with (startLatlng and endLatlng)
  6. endLatlng = startLatlng (see only after you call that draw method set the start value to end value)

注意

如果混淆,请使用实际值,并尝试了解我的解释. 第一次都一样. 因为它们都在同一位置,所以它会点一个点.(我想)现在startLatlng& endLatlng都具有第一个相同的值.

If it's confusing use real values and try to understand what i explained. First time both are same. It darws a dot because both are in the same spot.(i guess) Now startLatlng & endLatlng both have the first same value.

第二次新位置到来startLatlng更改.然后它不能转到该 boolean if方法,因为它是第一次.

Second time new location comes startLatlng changes.Then it cannot go to that boolean if method because it's only for the first time.

现在,它调用该方法绘制线条( new startLatlng old endLatlng).只有在调用该draw方法之后,您的endLatlng才会获得新的startLatlng值.

Now it call that method to draw line (new startLatlng and old endLatlng). Only after you call that draw method, your endLatlng gets that new startLatlng value.

但是,下一次当您再次用新数据调用完整逻辑时,新数据将分配给startLatlng.

But the next time when your full logic gets called with a very new data again that very new data assigns to startLatlng.

因此,它可以在startLatlng(新位置)和endLatlng(旧位置)之间画一条线.

So in this way it can draw a line between the startLatlng (new Possition) to endLatlng (oldPossition).

这篇关于随着用户移动,在Android谷歌地图中绘制一条折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆