有效的方式来获得使用Android的GPS路线点列表

问题描述:

我要借鉴谷歌地图的路线的方法。
此方法需要一个列表 LatitudeLongitude 对象,作为顾名思义,包含夫妻双纬度双经度

I have a method to draw a route on a Google Maps. This method needs a List of LatitudeLongitude objects that, as name suggests, contains couples of double latitude and double longitude.

现在我需要实现填充路线点列表的方法,这种方式:

Now I need to implement a method that fills the route points list, in this way:

该方法

protected void startFillList(){
  //to do
}

开始得到协调和运行过程中的列表中添加这些,

start to get coordinate and add these in the list during the movement,

该方法

protected List<LatitudeLongitude> stopFillList(){
  //to do
}

停下来得到新的坐标并返回结果列表

stop to get new coordinates and returns the result list

我怎么能这样做呢?

如果我用,不断地调用一段时间(直到停止条件达到)

If I use a while that invokes continuously (until stop condition reached)

locationManager.getLastKnownLocation(provider);

填充列表,应用程序挂起残忍。

to fill the list, the app hangs brutally.

如果我理解正确您的问题,你可以试试这个方法:

If I understand your issue correctly, you can try this way:

private ArrayList<LatLng> CoordsList = new ArrayList<>();
private static boolean addCoords = false;


protected void startFillList(){
   addCoords = true;
}

protected ArrayList<LatLng> stopFillList(){
   addCoords = false;
   return CoordsList; 
}


@Override
public void onLocationChanged(Location location) {

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

    // Creating a LatLng object and add to list
    currentCoods = new LatLng(latitude, longitude);

    //check condition if add coordinates
    if(addCoords)
        CoordsList.add(currentCoods);

}

希望这有助于!