位置使用LocationListener每100米更新一次

时间:2015-06-22 17:46:21

标签: android locationlistener

我是android新手。我正在尝试一个代码,每100米获得一次更新,即每100米的位置需要在我的应用TextView上更新。

这段代码是否足够,因为当我开车或骑自行车时,TetView没有正确更新。(从某种意义上说,我得到相同的位置,我需要关闭应用程序并重新打开它以获得新地点)

还有一个疑问是,在文件中写下最后知道的位置并稍后显示它是一个好习惯吗?如果我没有网络连接,我可以获取最后的已知位置。

我的代码:

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


    public static final String TAG=MainActivity.class.getSimpleName();
    UserLocationDetails locationDetails=new UserLocationDetails();
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
    GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    TextView userLocationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userLocationView=(TextView) findViewById(R.id.idLocationOfUser);

        mGoogleApiClient=new GoogleApiClient.Builder(this).
                addConnectionCallbacks(this).
                addOnConnectionFailedListener(this).
                addApi(LocationServices.API).build();
        mLocationRequest = LocationRequest.create().setSmallestDisplacement(100)
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
                .setInterval(10 * 1000)        // 10 seconds, in milliseconds
                .setFastestInterval(1 * 1000); // 1 second, in milliseconds


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(mGoogleApiClient.isConnected()){
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.i(TAG,"Location Service Connected");
        Location location=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

        if (location == null) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            Log.e(TAG,"Location is NULL");
        }
        else {
            handleNewLocation(location);
        }


    }

    private void handleNewLocation(Location location) {

        double latitude=location.getLatitude();
        double longitude=location.getLongitude();
        Log.i(TAG,"Latitude : "+location.getLatitude());
        Log.i(TAG,"Longitde : "+location.getLongitude());

        Log.i(TAG, "Location at : " + location.toString());

                locationDetails.setLocationDetailsOfUser(location.toString());

        userLocationView.setText(locationDetails.getLocationDetailsOfUser());
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG,"Location Service Suspended");

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } else {
            Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
        }

    }


    @Override
    public void onLocationChanged(Location location) {
        handleNewLocation(location);
    }
}

1 个答案:

答案 0 :(得分:0)

@Override
public void onLocationChanged(Location location) {
    if (location.distanceTo(mCurrentLocation) > 100)
    {
        // do update stuff
        mCurrentLocation = location;
    }
}

其中mCurrentLocation是最后一个已知位置。

您无法根据距离请求位置更新。