我在地图活动中获取位置时遇到问题。基本上,它获得了位置,但需要很长时间(几分钟)。我使用请求单个位置更新的单例来检索我的位置。这适用于我的应用程序中的多个其他实例。但是,当我在地图活动中尝试它时,需要很长时间。这是我用来获取位置的课程:
public class SingleShotLocationProvider {
public interface LocationCallback {
void onNewLocationAvailable(GPSCoordinates location);
}
public static void requestSingleUpdate(final Context context, final LocationCallback callback) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//If network is available
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.i("SingleShot", "network available");
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
try {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLongitude(), location.getLatitude()));
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override public void onProviderEnabled(String provider) {}
@Override public void onProviderDisabled(String provider) {}
}, null);
}
catch (SecurityException e){
Log.e("Location", "security exception");
}
}
//If GPS is enabled
else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.i("SingleShot", "GPS available");
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
try {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLongitude(), location.getLatitude()));
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override public void onProviderEnabled(String provider) {}
@Override public void onProviderDisabled(String provider) {}
}, null);
}
catch (SecurityException e){
Log.e("SingleShot", "security exception");
}
}
else{
Toast.makeText(context, "No network or GPS available. Try again later.", Toast.LENGTH_LONG).show();
}
}
public static class GPSCoordinates {
public float longitude = -1;
public float latitude = -1;
public GPSCoordinates(double lon, double lat) {
longitude = (float) lon;
latitude = (float) lat;
}
}
}
此课程适用于我的应用中的大多数区域。它适用于服务和片段。但是当我在我的地图活动(FragmentActivity)中使用它时,单例需要很长时间才能使用我发送它的回调。以下是我从地图活动中请求新位置的方法:
public class Traffic extends FragmentActivity
implements OnMapReadyCallback {
MapFragment mapFragment;
GoogleMap map;
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//...
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
SingleShotLocationProvider.requestSingleUpdate(getApplicationContext(), new SingleShotLocationProvider.LocationCallback() {
@Override
public void onNewLocationAvailable(SingleShotLocationProvider.GPSCoordinates location) {
Log.i(TAG, location.toString());
}
});
}
}
对于为什么会花这么长时间的任何想法?
答案 0 :(得分:0)
尝试将Criteria更改为COARSE或不太准确但足够的东西。
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
https://developer.android.com/reference/android/location/Criteria.html