Google Play服务或Android定位服务

Zap*_*ica 3 gps android google-play-services location-services

我想在我的应用程序中实现基于位置的功能.我一直在阅读,我发现自己有点困惑.

当谷歌搜索教程时,几乎每个结果都会返回一个使用Android Location API的示例.

但是在阅读android开发人员指南时,他们会说明以下内容:

Google Play服务位置API优先于Android框架位置API(android.location),作为向您的应用添加位置感知的一种方式.如果您当前正在使用Android框架位置API,强烈建议您尽快切换到Google Play服务位置API.

Android文档

所以这告诉我不要去实现一个位置监听器的简单路线.

所以我的问题是,两者有什么区别?我为什么要使用一个而不是另一个?

我在哪里可以找到关于如何安全准确地正确访问Google Play服务位置API的体面教程.

到目前为止我已尝试过这个(如Android网站上所建议的那样)但是我的回调都没有被调用.

public class LocationManager implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Context mContext;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;

public LocationManager(Context context) {
    mContext = context;
    //
    if (checkIfGooglePlayServicesAreAvailable()) {
        //Get Access to the google service api
        buildGoogleApiClient();
    } else {
        //Use Android Location Services
        //TODO:
    }
}

public Location getCoarseLocation() {
    if (mLastLocation != null) {
        return mLastLocation;
    } else return null;
}

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

private boolean checkIfGooglePlayServicesAreAvailable() {
    int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
    if (errorCode != ConnectionResult.SUCCESS) {
        GooglePlayServicesUtil.getErrorDialog(errorCode, (MainActivity) mContext, 0).show();
        return false;
    }
    return true;
}


@Override
public void onConnected(Bundle bundle) {
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location != null) {
        mLastLocation = location;
        Toast.makeText(mContext, location.getLongitude() + " , " + location.getLatitude() + " : " + location.getAccuracy(), Toast.LENGTH_LONG).show();
    }
}

@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(mContext, "suspended", Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Toast.makeText(mContext, connectionResult.toString(), Toast.LENGTH_LONG).show();
}
}
Run Code Online (Sandbox Code Playgroud)

然后我从我的活动中调用我的LocationManager:

LocationManager locationManager = new LocationManager(this);
Location location = locationManager.getCoarseLocation();
//Use Location
Run Code Online (Sandbox Code Playgroud)

我想创建一个帮助类,我可以从任何活动或片段调用它.但是,当我运行以下命令时,构造函数执行成功.但是我的回调中没有一个断点被击中.即使在1或2分钟后.

小智 5

Google Play服务Api使用回调方法public void onConnected(Bundle bundle),该方法仅在建立连接时调用.只有这样,LocationServices.FusedLocationApi.getLastLocation(googleApiClient)方法调用才能返回正确的Location.

您已构建了一个帮助程序类来获取此位置,但您应该了解该连接未立即建立.因此,只是对辅助类或其方法的异步调用可能会返回null对象,因为可能尚未建立连接.

您可以通过Location以下方式获得.

1)广播位置并通过a接收它BroadcastReceiver以从内部执行后续操作public void onConnected(Bundle bundle).这对我有用.

private boolean flag = false;

@Override
public void onConnected(Bundle connectionHint) {
    location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    if (location != null && !flag) {
        flag = true;
        Intent intent = new Intent(LOCATION_BROADCAST_ACTION);
        intent.putExtra(INTENT_EXTRA_LOCATION, location);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
        Log.i(TAG, "Sending Location Broadcast");
        Log.i(TAG, location.toString());
    } else if (location == null){
        Toast.makeText(mContext, R.string.no_location_detected, Toast.LENGTH_LONG).show();
        Log.e(TAG, "No Location Detected");
    }
}
Run Code Online (Sandbox Code Playgroud)

2)Location在调用Activity或Fragment中创建一个公共对象,并从该public void onConnected(Bundle bundle)方法中更新其值.

@Override
public void onConnected(Bundle connectionHint) {
    location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    if (location != null) {
        MyActivity.location = location; // update the Location object of you caller activity or fragment
        Log.i(TAG, "location updated");
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,您应该在构建GoogleApiClient后连接到Google Play服务位置Api.只需在构造函数中调用方法mGoogleApiClient.connect()后添加方法调用即可buildGoogleApiClient();.您无需检查Google Play服务是否可用.

您还可以googleApiClient.connect();在实现中添加方法调用public void onConnectionSuspended(int i).