简体   繁体   English

如何使用谷歌地图实时保存跟踪

[英]How to save a tracking in real time with google map

The idea is to create an ArrayList of locations and every time I save the location data in that list in the same time I draw a polyline between each two points. 这个想法是创建一个位置的ArrayList ,每次我将位置数据保存在该列表中的同时,我在每两个点之间绘制一条折线。

My question is about the Google Maps method onLocationChanged() . 我的问题是有关Google Maps方法onLocationChanged() I want to know if it is called automatically, I mean every time the location change, or if it is called when the user click on the button of get current location? 我想知道它是自动调用的,还是每次位置更改时调用,还是当用户单击获取当前位置的按钮时调用它?

If someone has a better idea please let me know? 如果有人有更好的主意,请告诉我?

Basically onLocationChanged is being called everytime the location is changed. 基本上,每次位置更改时都会调用onLocationChanged。 However it's bad idea to implement your code inside it, you can do something like that 但是,在其中实现代码不是一个好主意,您可以执行类似的操作

public class GPSTracker  implements LocationListener{
private Location location ;
public Location getLocation(){
return this.location;
}
// .. code

 @Override
    public void onLocationChanged(Location location) {
    this.location=location;
    }

}

now in your main activity you can get location by making an object of GPSTracker then like this 现在在您的主要活动中,您可以通过创建GPSTracker对象来获取位置,然后像这样

GPSTracker=new GPSTracker(this);
gpsTracker.getLocation (); //this will provide you with longitude,latitude then you can control it with a thread or something like that, by calling getLongitude,getLatitude you can get the location paramter

be noted the constructor takes context as a parameter so pass the context of your activity to the GPSTracker class please check the following link 请注意,构造函数将上下文作为参数,因此将您活动的上下文传递给GPSTracker类,请检查以下链接

As you've not provided your code, I don't know that you have a separate class for getting Location or in the same activity/class. 由于您未提供代码,因此我不知道您有一个单独的类来获取Location或在同一活动/类中。

I am assuming that you want to insert your updated location in an Arraylist and want to create a polyline connection all those points. 我假设您要在Arraylist插入更新的位置,并希望创建所有这些点的折线连接。

if I'm right, Here's what you can do. 如果我是对的,这就是您可以做的。

  • Create an Arraylist global variable in your Activity 在活动中创建一个Arraylist全局变量

     ArrayList<LatLng> cordList = new ArrayList<>(); 
  • Create a function/method call in onLocationChanged() onLocationChanged()创建函数/方法调用

     locationMarkers(location); //passing updated location to the function/method. 

    (If you have a separate class for getting location then get the reference of your activity in it and call this method using that reference.) (如果您有一个单独的类来获取位置,请在其中获取您的活动的引用,并使用该引用来调用此方法。)

  • Then create this method in your activity. 然后在您的活动中创建此方法。

     public void locationMarkers(Location location){ cordList.add(new LatLng(location.getLatitude(), location.getLongitude())); Polyline line =googleMap.addPolyline(newPolylineOptions().width(6).color(Color.GREEN)); line.setPoints(cordList); } 

This way you'll have a polyline on the map of your locations. 这样,您将在位置地图上有一条折线。
Also this polyline will be a straight line between two markers/positions. 同样,该折线将是两个标记/位置之间的直线。
If you want it to be on the road, use HashMap . 如果您希望它在路上,请使用HashMap

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

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