2013-03-05 106 views
2

我一直在尋找圍繞如何實現的方式在Android中獲得用戶的位置,我碰到這太問題就來了: What is the simplest and most robust way to get the user's current location on Android?LocationResult中發生了什麼?

我的問題是,什麼是LocationResult?我將如何使用它?我在那個問題上構建了一個類似的應用程序,我根據當前位置搜索附近的地方。我正在考慮有一個類似於輪詢位置更改的單獨類,所以我不必在不同的活動中重複該代碼。我懷疑LocationResult是爲了這個目的?你能告訴我一個如何在OnCreate()方法中使用它的例子嗎?

+0

這個問題和它的答案是將近三十歲,在這一點上。在Android年代,這是一個次要的永恆。您可以考慮閱讀文檔以獲取更新的指導:http://developer.android.com/guide/topics/location/strategies.html – CommonsWare 2013-03-05 01:35:31

+0

您是否發現了某些內容? – Castiblanco 2013-09-10 18:21:14

回答

0

Johnathan,

LocationResult是一種檢索要處理的數據的簡單方法。這是一個抽象類,您可以在其中實現子類以使用數據。見abstract class concept

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);