在 Android 中使用 gps 获取路线点列表的有效方法

Effective way to get the route point list using gps in Android

我有一种在 Google 地图上绘制路线的方法。 此方法需要 ListLatitudeLongitude 个对象,顾名思义,这些对象包含一对 double latitudedouble longitude.

现在我需要实现一个填充路线点列表的方法,这样:

方法

protected void startFillList(){
  //to do
}

开始获取坐标并在移动过程中将这些添加到列表中,

方法

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

停止获取新坐标和returns结果列表

我该怎么做?

如果我使用 while 连续调用(直到达到停止条件)

locationManager.getLastKnownLocation(provider);

为了填满列表,应用程序残酷地挂起。

如果我对你的问题理解正确,你可以这样试试:

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);

}

希望对您有所帮助!

看看

https://developer.android.com/training/location/receive-location-updates.html

public class MyActivity extends ActionBarActivity implements
     GoogleApiClient.ConnectionCallbacks,
     GoogleApiClient.OnConnectionFailedListener,
     com.google.android.gms.location.LocationListener  
{

LocationManager locationManager;
GoogleApiClient googleApiClient;

List<LatitudeLongitude> myPositions=new List<LatitudeLongitude>();
//Update Position after 2 minutes
private static final int UPDATE_POS_AFTER_MILLIS=120000;
@Override
protected void onResume() 
{
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    startFillList()

}

protected void startFillList(){
    buildGoogleApiClient();
    googleApiClient.connect();
}

protected synchronized void buildGoogleApiClient() 
{
    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

@Override
public void onConnected(Bundle bundle)
{
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
    {
        if(googleApiClient.isConnected())
        {
            currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
            if(currentLocation!=null)
            {
                LatLng currentLatLng = new LatLng(
                    (float) currentLocation.getLatitude(),
                    (float) currentLocation.getLongitude());
                myPositions.add(currentLatLng);
                //This is for periodically request
                createLocationRequest();
            }
        }
        else
        {
            googleApiClient.connect();
        }
    }
    else
    {
        //Notify User GPS is disabled
    }
}

protected void createLocationRequest() {
    locationRequest = new LocationRequest();
    locationRequest.setInterval(UPDATE_POS_AFTER_MILLIS);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationServices.FusedLocationApi.requestLocationUpdates(
            googleApiClient, locationRequest, this);
}
//After 2 Minutes this method will be called
@Override
public void onLocationChanged(Location location) {
    LatLng currentLatLng = new LatLng(
                    (float) currentLocation.getLatitude(),
                    (float) currentLocation.getLongitude());
    myPositions.add(currentLatLng);
}

protected List<LatitudeLongitude> stopFillList(){
    LocationServices.FusedLocationApi.removeLocationUpdates(
            googleApiClient, this);
    return myPositions;
}

}

别忘了实现所有接口方法