Learn about user speed in android using GPS

I tried to figure out the user's speed while the user is moving with the device. Here I followed one link with sample code. ie: Here is SpeedDemo.

The problem is that with the location.getSpeed() method we find the speed. So, for this I changed the location values ​​on the device, but every time the location.getSpeed() value returns only "0". Why is this happening, even changing location.

Can anyone find out about this?

+6
source share
3 answers

You need to keep track of location updates, and when you run the onLocationChanged(Location location) method, you can call location.getSpeed(); This will give you the correct speed if your phone really moves.

But if you test it on a Simulator and send the location to the emulator controller, it will always return 0.

Updated with Example

 public class LocationService implements LocationListener { LocationService locationService; LocationManager locationManager; Location lastLocation; private final String TAG = "LocationService" ; private final String MOCK_LOCAION_PROVIDER = "FAKE_PROVIDER"; LocationService(Context ctx) { LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); String mocLocationProvider = MOCK_LOCAION_PROVIDER; if (locationManager.getProvider(mocLocationProvider) != null) { locationManager.removeTestProvider(mocLocationProvider); } locationManager.addTestProvider(mocLocationProvider, false, false, false, false, true, true, true, 0, 5); locationManager.setTestProviderEnabled(mocLocationProvider, true); locationManager.requestLocationUpdates(mocLocationProvider, 0, 0, this); try { List<String> data = new ArrayList<String>(); InputStream is = ctx.getAssets().open("data.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = reader.readLine()) != null) { data.add(line); } // Log.e(TAG, data.size() + " lines"); new MockLocationProvider(locationManager, mocLocationProvider, data).start(); } catch (IOException e) { e.printStackTrace(); } } class MockLocationProvider extends Thread { private List<String> data; private LocationManager locationManager; private String mocLocationProvider; private String LOG_TAG = "MockLocationProvider"; public MockLocationProvider(LocationManager locationManager, String mocLocationProvider, List<String> data) throws IOException { this.locationManager = locationManager; this.mocLocationProvider = mocLocationProvider; this.data = data; } @Override public void run() { for (String str : data) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } // Set one position String[] parts = str.split(","); Double latitude = Double.valueOf(parts[0]); Double longitude = Double.valueOf(parts[1]); float speed = Float.valueOf(parts[2]); Location location = new Location(mocLocationProvider); location.setLatitude(latitude); location.setLongitude(longitude); location.setSpeed(speed); // location.setAltitude(altitude); // Log.d(LOG_TAG, location.toString()); // set the time in the location. If the time on this location // matches the time on the one in the previous set call, it will // be // ignored location.setTime(System.currentTimeMillis()); locationManager.setTestProviderLocation(mocLocationProvider, location); } } } public void onLocationChanged(Location location) { // TODO Auto-generated method stub Log.e(TAG, "onLocationChanged"); // Get Location Speed Here Log.d(TAG, "Speed " +location.getSpeed()); } public void onProviderDisabled(String provider) { // TODO Auto-generated method stub // Log.e(TAG, "onProviderDisabled : "+provider); } public void onProviderEnabled(String provider) { // TODO Auto-generated method stub // Log.e(TAG, "onProviderEnabled : "+provider); } public void onStatusChanged(String provider, int status, Bundle arg2) { // TODO Auto-generated method stub // Log.e(TAG, "onStatusChanged : "+status); } } 

The above code is actually a location service class, when you make an instance of this class, it registers a fake location service provider (except GPS and network) and enters some fake location parameters from a given file.

Below is the data.txt file, which has latitude,longitude,speed , the above class reads this data.txt file and enters fake lat, lon and speed at the location, as well as the start time for changing the location Thread.sleep() is also implemented.

Data.txt File

 24.856449265609735,67.04308920288086,1.64 24.856749265609735,67.04408920288086,7.64 24.856949265609735,67.04508920288086,11.64 24.857649265609735,67.04716920288086,13.64 24.857949265609735,67.04736920288086,12.64 24.857949265609735,67.04742520288086,8.64 24.857949265609735,67.04747020288086,4.64 24.856749265609735,67.04408920288086,6.11 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.1 24.856949265609735,67.04508920288086,2.13 24.857249265609735,67.04608920288086,0.6 24.856949265609735,67.04508920288086,1.19 24.857249265609735,67.04608920288086,1.6 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.15 24.857849265609735,67.04729920288086,17.64 24.857949265609735,67.04736920288086,12.64 24.857949265609735,67.04739920288086,16.64 24.857949265609735,67.04742520288086,8.64 24.857949265609735,67.04747020288086,4.64 24.856749265609735,67.04408920288086,6.11 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.1 24.856949265609735,67.04508920288086,2.13 24.857249265609735,67.04608920288086,0.6 24.856949265609735,67.04508920288086,1.19 24.857249265609735,67.04608920288086,1.6 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.15 24.857849265609735,67.04729920288086,17.64 24.857949265609735,67.04736920288086,12.64 24.857949265609735,67.04739920288086,16.64 24.857949265609735,67.04742520288086,8.64 24.857949265609735,67.04747020288086,4.64 24.856749265609735,67.04408920288086,6.11 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.1 24.857849265609735,67.04729920288086,17.64 24.857949265609735,67.04736920288086,12.64 24.857949265609735,67.04739920288086,16.64 24.857949265609735,67.04742520288086,8.64 24.857949265609735,67.04747020288086,4.64 24.856749265609735,67.04408920288086,6.11 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.15 24.856949265609735,67.04508920288086,2.13 24.857249265609735,67.04608920288086,0.6 24.856949265609735,67.04508920288086,1.19 24.857249265609735,67.04608920288086,1.6 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.15 24.856949265609735,67.04508920288086,2.13 24.857249265609735,67.04608920288086,0.6 24.856949265609735,67.04508920288086,1.19 24.857249265609735,67.04608920288086,1.6 24.856949265609735,67.04508920288086,2.12 24.857249265609735,67.04608920288086,1.15 
+7
source

I think that getSpeed ​​() will only work if there is a GPS speed component in a specific place. Otherwise, it will return 0.0 all the time.

You can check for this using the location.hasSpeed ​​() method. If this is true, then your things still think differently.

+1
source

First we need to check if GPS is enabled or not using the location manager.

 isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

Then you need to implement GpsStatus.Listener to get Gps updates. It will return GPS location updates.

 GPSManager.locationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { if (GPSManager.gpsCallback != null) { GPSManager.gpsCallback.onGPSUpdate(location); } } @Override public void onProviderDisabled(final String provider) { } @Override public void onProviderEnabled(final String provider) { } @Override public void onStatusChanged(final String provider, final int status, final Bundle extras) { } }; 

From this place you can get the current speed.

 speed = location.getSpeed(); 

Refer to my site here for more information: http://velmm.com/get-current-user-speed-using-gps-android/

-1
source

Source: https://habr.com/ru/post/922273/


All Articles