1

我有以下代码可以获取设备的当前位置:

public class MapPlace extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
{
    private LocationRequest mLocationRequest;
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    // location coordinates
    double dLatitude = 0.0;
    double dLongitude = 0.0;

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

          if (mGoogleApiClient == null || !mGoogleApiClient.isConnected())
          {
               buildGoogleApiClient();
               mGoogleApiClient.connect();
          }

          MapFragment mapF = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
         if (mapF != null)
            mapF.getMapAsync(this);
        }

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

        @Override
        public void onConnected(Bundle bundle) {
           mLocationRequest = new LocationRequest();
           mLocationRequest.setInterval(1000);
           mLocationRequest.setFastestInterval(1000);
           mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
           LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
         }

         @Override
         public void onConnectionSuspended(int i) {}

         @Override
         public void onConnectionFailed(ConnectionResult connectionResult) {}

         @Override
         public void onLocationChanged(Location location)
         {
             mLastLocation = location;
             dLatitude = mLastLocation.getLatitude();
             dLongitude = mLastLocation.getLongitude();

         }
    }
}

此代码工作正常,但在某些设备中无法正常工作,要使用此代码获得成功,我必须重新启动设备或关闭并重新打开活动,所以我想避免这个问题。

作为推荐这个堆栈溢出的原因,我试图迁移到 LocationFusedLocation 来解决这个问题,但是我在 java 类中添加 GooglePlayServicesClient 实现时遇到了一些问题,因为正如这个链接所说,这个库暂时被弃用了。那么……我该怎么办?还有其他方法吗?

4

1 回答 1

0

看起来您已经在使用 GoogleApiClient,因此您不需要任何帮助。您的代码已经更新为不推荐使用的方式。

只需进行几处更改即可解决您的问题,我还看到一个未提及的问题。

首先,您提到您在使用 Location 侦听器时遇到问题,并且您需要重新启动 Activity 以使其再次工作。

要解决这个问题,只需将连接 GoogleApiClient 的代码从 onCreate() 移动到 onResume():

@Override
public void onResume() {
    super.onResume();

    if (mGoogleApiClient == null || !mGoogleApiClient.isConnected())
    {
        buildGoogleApiClient();
        mGoogleApiClient.connect();
    }

    MapFragment mapF = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    //only call getMapAsync() again if mMap is null:
    if (mapF != null && mMap == null)
        mapF.getMapAsync(this);

}

另一个问题是您应该在 onPause() 中取消注册位置更新,这样当用户离开您的应用时,您的应用就不会继续请求位置更新:

@Override
public void onPause() {
    super.onPause();

    //added to stop location updates when Activity is no longer active
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}

这是应该为您工作的完整类代码,请注意,我还添加了一个 GoogleMap 引用,并将其设置在 onMapReady() 回调中(您可能已经在您的完整代码中拥有它):

public class MapPlace extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener
{
    private LocationRequest mLocationRequest;
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    // location coordinates
    double dLatitude = 0.0;
    double dLongitude = 0.0;

    GoogleMap mMap;

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

    }

    @Override
    public void onResume() {
        super.onResume();

        if (mGoogleApiClient == null || !mGoogleApiClient.isConnected())
        {
            buildGoogleApiClient();
            mGoogleApiClient.connect();
        }

        MapFragment mapF = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        //only call getMapAsync() again if mMap is null:
        if (mapF != null && mMap == null)
            mapF.getMapAsync(this);

    }

    @Override
    public void onPause() {
        super.onPause();

        //added to stop location updates when Activity is no longer active
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    }

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

    @Override
    public void onConnected(Bundle bundle) {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onConnectionSuspended(int i) {}

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {}

    @Override
    public void onLocationChanged(Location location)
    {
        mLastLocation = location;
        dLatitude = mLastLocation.getLatitude();
        dLongitude = mLastLocation.getLongitude();

    }

}
于 2015-08-03T17:39:52.947 回答