我正在尝试使用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扩展了Application {
@Override public void 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" />
我知道它正在运行,因为每次活动运行时我都会通过TOAST显示信息...我每次用户打开应用程序时都会尝试获取当前位置。我想我错过了它是如何工作的。我认为latestInfo.refresh()会给我最新信息,但是,如果我移动到200米外的另一个位置,我仍然得到相同的lat / lng和相同的.lastLocationUpdateTimestamp。
我想获取当前用户位置,然后定期获取更新。这个库看起来很完美......但是我没有得到结果。我没有正确实现这个吗?
EDIT ::::
我没有实现广播接收器,但是,我不知道如何让它适用于多个课程。我有3个不同的要求用户位置......
答案 0 :(得分:1)
refresh()方法只是更新当前的latestInfo对象。你需要要求更新。
LocationLibrary.forceLocationUpdate(MainActivity.this);
你需要让libary在几秒钟内获取当前位置。然后你可以打电话
latestInfo.LastLat
并显示。
为库实现广播允许您访问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" />