簡體   English   中英

Google Play服務或Android定位服務

[英]Google Play Services or Android 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();
}
}

然后我從我的活動中調用我的LocationManager:

LocationManager locationManager = new LocationManager(this);
Location location = locationManager.getCoarseLocation();
//Use Location

我想創建一個幫助類,我可以從任何活動或片段調用它。 但是,當我運行以下命令時,構造函數執行成功。 但是我的回調中沒有一個斷點被擊中。 即使在1或2分鍾后。

Google Play服務Api使用一個回調方法public void onConnected(Bundle bundle) ,只有在建立連接時才會調用它。 只有這樣, LocationServices.FusedLocationApi.getLastLocation(googleApiClient)方法調用才能返回正確的Location

您已構建了一個幫助程序類來獲取此位置,但您應該了解該連接未立即建立。 因此,只是對輔助類或其方法的異步調用可能會返回null對象,因為可能尚未建立連接。

您可以通過以下方式獲取Location

1)廣播位置並通過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");
    }
}

2)在調用Activity或Fragment中創建一個公共Location對象,並從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");
    }
}

此外,您應該在構建GoogleApiClient后連接到Google Play服務位置Api。 只需在mGoogleApiClient.connect()之后添加方法調用buildGoogleApiClient(); 在構造函數中調用方法。 您無需檢查Google Play服務是否可用。

您還可以添加方法調用googleApiClient.connect(); 在你的public void onConnectionSuspended(int i)

你需要打電話:

mGoogleApiClient.connect()

除了您對差異的疑問之外:Google Play服務框架還為您提供了“融合”的位置。 使用Android框架,您應該使用GPS或Wif。 您可以指定條件,但如果您需要位置用戶位置,則Google Play服務是最佳選擇。 如果您需要一個非常精確的位置,例如GPS,例如您的應用程序是導航器應用程序,那么最好直接使用GPS。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM