2014-01-12 23 views
0

我試圖使用Little Fluffy Location Library爲了獲取我的小應用程序的位置信息。我有它在異步過程中運行,如下所示:小毛茸茸的位置 - 誤解如何獲得當前位置

private class ShowLocationTask extends AsyncTask<Void, Void, Boolean> { 
     @Override 
     protected Boolean doInBackground(Void... params) { 
      //LocationLibrary.forceLocationUpdate(getApplicationContext()); 
      latestInfo = new LocationInfo(getBaseContext()); 
      latestInfo.refresh(getBaseContext()); 

      lat = latestInfo.lastLat; 
      lng = latestInfo.lastLong; 

      return true; 
     } 

     protected void onPostExecute(Boolean result) { 
      if (result != true) { 
       Toast.makeText(ACTIVITY.this, 
         "Cannot get your location...", Toast.LENGTH_LONG) 
         .show(); 
      } else { 

       Date date = new Date(latestInfo.lastLocationUpdateTimestamp); 

       Toast.makeText(ACTIVITY.this, 
         "Last Updated: " + date, Toast.LENGTH_LONG).show(); 
       LatLng meLoc = new LatLng(lat, lng); 
       CameraPosition cameraPosition = new CameraPosition.Builder() 
         .target(meLoc) // Sets the center of the map to 
         .zoom(ZP).bearing(0).tilt(80).build(); 
       map.animateCamera(CameraUpdateFactory 
         .newCameraPosition(cameraPosition)); 
       Toast.makeText(ACTIVITY.this, 
         "..." + lat + " : " + lng, Toast.LENGTH_LONG).show(); 
       Log.e("LATLON", lat + " : " + lng); 
      } 
     } 

    } 

我在我的onCreate()中調用它。

userLoc.execute();

我延伸我的應用程序與此類:

公共類FluffyLocation延伸申請{

@覆蓋公共無效的onCreate(){super.onCreate();

LocationLibrary.showDebugOutput(true); 

    try {   LocationLibrary.initialiseLibrary(getBaseContext(), 0, 
       2 * 60 * 1000, "com.jasonflaherty.snoteldata");   } catch (UnsupportedOperationException ex) {   Log.d("Application", 
       "UnsupportedOperationException thrown - the device doesn't have any location providers");  } } 

}

設置的清單:

<receiver 
     android:name="com.littlefluffytoys.littlefluffylocationlibrary.StartupBroadcastReceiver" 
     android:exported="true" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
    <receiver 
     android:name="com.littlefluffytoys.littlefluffylocationlibrary.PassiveLocationChangedReceiver" 
     android:exported="true" /> 

我知道它正在運行,因爲我得到的觥籌交錯時顯示信息活動運行...我正在嘗試獲取當前的loc每當用戶打開應用程序時,我想我錯過了這是如何工作的。我認爲latestInfo.refresh()會給我最新的信息,但是,如果我移動到200米外的另一個位置,我仍然會得到相同的經緯度,並且也是相同的.lastLocationUpdateTimestamp。

我想獲取當前用戶位置,然後定期獲取更新。這個庫似乎是完美的,但是我沒有得到結果。我沒有正確實施?

編輯::::

我並沒有實現廣播接收器,但是,我不知道如何得到它超過一類的工作。我有3個不同的會要求用戶的位置...

回答

1

refresh()方法只是更新當前latestInfo對象。你需要詢問更新。

LocationLibrary.forceLocationUpdate(MainActivity.this); 

並且您需要讓libary獲得當前位置一秒。那麼你可以撥打

latestInfo.LastLat 

和顯示。

實現庫的Broadcast可讓您訪問方法OnLocationChanged,該方法在庫的LocationListenner獲取新位置時調用。

public class FluffyBroadcastReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    Log.d("FluffyBroadcastReceiver", "onReceive(): received location update"); 

    final LocationInfo locationInfo = (LocationInfo) intent.getSerializableExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO); 
    //do something whit the new location   
    doSomething(String.valueOf(locationInfo.lastLong),String.valueOf(locationInfo.lastLat),String.valueOf(locationInfo.lastAccuracy)); 
} 
} 

,並在清單文件,你必須定義這個接收器

<receiver android:name="com.YOURPACKAGE.FluffyBroadcastReceiver" android:exported="true" />