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

时间:2015-03-21 12:40:21

标签: android google-maps gps android-maps android-location

我有一种在Google地图上绘制路线的方法。 此方法需要List LatitudeLongitudedouble latitude个对象,顾名思义,这些对象包含double longitudeprotected void startFillList(){ //to do } 两对。

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

方法

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

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

方法

locationManager.getLastKnownLocation(provider);

停止获取新坐标并返回结果列表

我怎么能这样做?

如果我使用一段时间连续调用(直到达到停止条件)

{{1}}

要填写列表,应用程序会残酷地挂起。

2 个答案:

答案 0 :(得分:3)

如果我理解您的问题,您可以尝试这种方式:

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

}

希望这有帮助!

答案 1 :(得分:0)

看看

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

}

不要忘记实现所有接口方法