Android - Getting Network GPS location within a short time frame (10 seconds max)

耗尽温柔 提交于 2019-12-06 09:50:00

Well what you are looking for is to ask the device for the best criteria to fetch a coarse location.

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);  // Faster, no GPS fix.
String provider = locationManager.getBestProvider(criteria, true); // only retrieve enabled providers.

Then just register a listener

locationManager.requestLocationUpdates(provider, ITERATION_TIMEOUT_STEP, MIN_LOCATION_UPDATE_DISTANCE, listener); //listener just implements android.location.LocationListener

in the listener you receive an update as such

 void onLocationChanged(Location location) {
     accuracy = location.getAccuracy(); //accuracy of the fix in meters
     timestamp = location.getTime(); //basically what you get from System.currentTimeMillis()
 }

At this point my advice is to sort solely based on accuracy as you can't vary your location that much in 10 seconds yet coarse location updates vary greatly in accuracy.

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!