LocationResult发生了什么?

时间:2013-03-05 01:29:45

标签: android google-maps gps location

我一直在寻找如何实现在Android中获取用户位置的方法,我遇到了这个问题: What is the simplest and most robust way to get the user's current location on Android?

我的问题是,什么是LocationResult?我该如何使用它?我正在该问题中构建一个类似的应用程序,我根据当前位置搜索附近的地方。我正在考虑有一个单独的类来轮询位置更改,所以我不必在不同的活动中重复代码。我怀疑LocationResult是为了这个目的吗?你能告诉我和在OnCreate()方法中如何在2个活动中使用它的例子吗?

2 个答案:

答案 0 :(得分:0)

乔纳森,

LocationResult是一种检索要处理的数据的简单方法。它是一个抽象类,您可以在其中实现子类以使用数据。请参阅abstract class concept

答案 1 :(得分:0)

这就是你需要的代码:

    protected LocationCallback mLocationCallback = new LocationCallback()
{
    @Override
    public void onLocationAvailability(LocationAvailability locationAvailability) {
        super.onLocationAvailability(locationAvailability);
        if(!locationAvailability.isLocationAvailable())
        {// No location available
            checkLocationSettings();
            Snackbar snackLocationUnavailable = Snackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.location_unavailable, Snackbar.LENGTH_LONG);
            snackLocationUnavailable.show();
        }
        else
        {
            Snackbar snackLocationAvailable = Snackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.location_available, Snackbar.LENGTH_LONG);
            snackLocationAvailable.show();
        }
}

    @Override
    public void onLocationResult(LocationResult locationResult) {
        super.onLocationResult(locationResult); 
        mLastLocation = locationResult.getLastLocation();
        mMyLatLng = new LatLng(valueOf(mLastLocation.getLatitude()), valueOf(mLastLocation.getLongitude()));
        meMarker.setPosition(mMyLatLng);
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mMyLatLng, 15));
        Snackbar snak2 = Snackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.location_updated, Snackbar.LENGTH_INDEFINITE);
        snak2.show();
    }
};

...

onCreate(){
// Build GoogleApiClient
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

//`Build LocationRequest
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(2000)        // 2 seconds, in milliseconds
                .setFastestInterval(1000); // 1 second, in milliseconds
`
//     

Build LocationSettingsRequest
        LocationSettingsRequest.Builder lRbuilder = new LocationSettingsRequest.Builder();
        lRbuilder.addLocationRequest(mLocationRequest);
        mLocationSettingsRequest = lRbuilder.build();
        lRbuilder.setAlwaysShow(true);

// ...

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mLocationCallback, null);

//...

 LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mLocationCallback);