更新后如何获取位置?

时间:2015-06-19 19:16:46

标签: java android google-maps

我按照教程使用谷歌地图API来获取最后的已知位置或请求位置更新(如果没有这样的位置)。

这是我的onConnected方法

WDF_REQUEST_SEND_OPTION_SEND_AND_FORGET

所以第一个条件应该是请求一个新的位置。这个位置存放在哪里?我以为它可能会执行handeNewLocation(代码如下)

@Override
public void onConnected(Bundle bundle) {
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location == null) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        Log.v(TAG, "old location" + location);

    }
    else {
        handleNewLocation(location);
        Log.v(TAG, "new location");
    }
}

我需要获取位置并将其存储为字符串,所以我的问题是我不知道到底在哪里

private void handleNewLocation(Location location) {
    Log.d(TAG, location.toString());
    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();
    loc = "Latitude: " + Double.toString(currentLatitude) + ", Longitude: " + Double.toString(currentLongitude);
    Log.v(TAG, loc);
}

正在保存新位置。

这是清单:

LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

<?xml version="1.0" encoding="utf-8"?>

2 个答案:

答案 0 :(得分:1)

您需要致电

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    Log.v(TAG, "old location" + location);

之前打电话

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

然后该位置将存储在location

答案 1 :(得分:1)

请求位置更新的调用就是这样,它会请求位置更新。

拨打电话后,新地点将进入onLocationChanged()

如果您希望始终保存当前位置,可以使用成员变量。

请注意,无需像现在一样记录空位,因此在位置为null的情况下删除日志条目:

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location == null) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
    else {
        handleNewLocation(location);
        Log.v(TAG, "new location");
    }

然后在handleNewLocation()中更新成员变量:

private void handleNewLocation(Location location) {
    Log.d(TAG, location.toString());
    currentLatitude = location.getLatitude(); //update member variable
    currentLongitude = location.getLongitude(); //update member variable
    loc = "Latitude: " + Double.toString(currentLatitude) + ", Longitude: " + Double.toString(currentLongitude);
    Log.v(TAG, loc);
}

然后,在handleNewLocation()回调中致电onLocationChanged()

@Override
public void onLocationChanged(Location location) {

    //location changed, update current location
    handleNewLocation(location);
    Log.v(TAG, "new location");

}

全班:

public class LocationActivity extends Activity implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    double currentLatitude; //added as member variable
    double currentLongitude; //added as member variable
    String TAG = "myapp";
    String loc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buildGoogleApiClient();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onPause(){
        super.onPause();
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }

    protected synchronized void buildGoogleApiClient() {
        Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10);
        mLocationRequest.setFastestInterval(10);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(0.1F);

        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location == null) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
        else {
            handleNewLocation(location);
            Log.v(TAG, "new location");
        }

    }

    private void handleNewLocation(Location location) {
        Log.d(TAG, location.toString());
        currentLatitude = location.getLatitude(); //update member variable
        currentLongitude = location.getLongitude(); //update member variable
        loc = "Latitude: " + Double.toString(currentLatitude) + ", Longitude: " + Double.toString(currentLongitude);
        Log.v(TAG, loc);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLocationChanged(Location location) {

        //location changed, update current location
        handleNewLocation(location);
        Log.v(TAG, "new location");

    }
}