cs5530 mobile wireless systems gps location in android
play

CS5530 Mobile/Wireless Systems GPS/Location in Android Yanyan - PowerPoint PPT Presentation

CS5530 Mobile/Wireless Systems GPS/Location in Android Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang UC. Colorado Springs CS5530 Ref. CN5E, NT@UW, WUSTL Android Location Interface 1. Request permission in


  1. CS5530 Mobile/Wireless Systems GPS/Location in Android Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang UC. Colorado Springs CS5530 Ref. CN5E, NT@UW, WUSTL

  2. Android Location Interface 1. Request permission in AndroidManifest.xml and code (Marshmallow+) 2. Create a Location Manager 3. Register a listener with the Location Manager to receive location updates 4. Define the listener that responds to location updates (similar to IntentFilter and registerReceiver) CS5530 2 Ref. CN5E, NT@UW, WUSTL

  3. Android Location Interface • Permission <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" o /> <!-- Needed only if your app targets Android 5.0 (API level 21) or higher. --> <uses-feature android:name="android.hardware.location.gps" /> If app targets Android 5.0 (API level 21) or higher, must declare that your app o uses android.hardware.location.gps hardware feature in the manifest file CS5530 3 Ref. CN5E, NT@UW, WUSTL

  4. Android Location Interface • Create a location manager o LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); • Before requesting location updates, must check permission o if (checkPermission(this)) { locationManager.requestLocationUpdates(….); } else{ ActivityCompat.requestPermissions(this, …); } CS5530 4 Ref. CN5E, NT@UW, WUSTL

  5. Android Location Interface • Before requesting location updates, checking permission o public static boolean checkPermission(final Context context) { return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED; } o if (checkPermission(this)) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener); locationManager.addGpsStatusListener(gpsStatusListener); }else{ ActivityCompat.requestPermissions(this, PERMS_INITIAL, 111); } CS5530 5 Ref. CN5E, NT@UW, WUSTL

  6. Android Location Interface • Register a listener (to request location updates) o locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener ); // The minimum distance to change Updates in meters o private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 m // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute CS5530 6 Ref. CN5E, NT@UW, WUSTL

  7. Android Location Interface Define the listener that responds to location updates • LocationListener locationListener = new LocationListener() { o @Override public void onLocationChanged(Location location) { if (location != null) myLocation = location; } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; CS5530 7 Ref. CN5E, NT@UW, WUSTL

  8. Android Location Interface • Get information about satellites o Register a satellite listener } locationManager.addGpsStatusListener( gpsStatusListener ) o Implement gpsStatusListener to respond to satellite status changes } private GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() { @Override public void onGpsStatusChanged(int event) { …... } }; CS5530 8 Ref. CN5E, NT@UW, WUSTL

  9. Android Location Interface • GPS satellite listener private GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() { o @Override public void onGpsStatusChanged(int event) { if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS){ if (checkPermission(getApplicationContext())) { GpsStatus gpsstatus = locationManager.getGpsStatus(null); Iterable<GpsSatellite> gpsit = gpsstatus.getSatellites(); int numsat = 0; for (GpsSatellite sat : gpsit) { Log.v(TAG, Float.toString(sat.getAzimuth())); Log.v(TAG, Float.toString(sat.getElevation())); Log.v(TAG, Integer.toString(sat.getPrn())); Log.v(TAG, Float.toString(sat.getSnr())); numsat++; } } } } }; CS5530 9 Ref. CN5E, NT@UW, WUSTL

  10. Android Location Interface • float getAzimuth() o Returns the azimuth of the satellite in degrees. The azimuth can vary between 0 and 360 • float getElevation() o Returns the elevation of the satellite in degrees. The elevation can vary between 0 and 90 • int getPrn() o Returns the PRN (pseudo-random number) for the satellite • float getSnr() o Returns the signal to noise ratio for the satellite. CS5530 10 Ref. CN5E, NT@UW, WUSTL

Recommend


More recommend