2014-01-12 22 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" />