180 likes | 279 Views
Introducing to Location in Android. Location is everything. Kamil Lelonek Kamil.Lelonek@student.pwr.wroc.pl. What we gonna talk about ?. Introduction to GEOgraphy How we „ describe ” the Earth? How we calculate coordinates ? Important issues to avoid problems
E N D
Introducing to Location in Android Locationiseverything Kamil Lelonek Kamil.Lelonek@student.pwr.wroc.pl
What we gonna talk about? • Introduction to GEOgraphy • How we „describe” the Earth? • How we calculatecoordinates? • Importantissues to avoidproblems • Findinguserlocation • Prepare to coding • Ways to obtaincurrentposition • Thistime emulator ishelpful • Developing Mapsapplication • GeoCoding • Goodold Google Maps API v1 • New look and feelGoogle Maps API v2 • Summary • Navigation • PRO tips for advancedusers • Q&A
Let’sgo backto school (N) West (W) East (E) º - degrees ’ - minutes ’’ - seconds (S)
How to calculateit? 23º25’24’’ S 23º25’24’’ S → – 23,439167 (double) (int) = 1E6 * (double)→ – 23439167 (int)
Attention Problems Extras • Positive and Negativevalues • Latutudegoesfirst • Altitude • Azimuth • Speed Calcuationproblems
Get prepared • Permissions • ACCESS_MOCK_LOCATION - emulator • ACCESS_COARSE_LOCATION - approximate • ACCESS_FINE_LOCATION - precise • ACCESS_LOCATION_EXTRA_COMMANDS • INSTALL_LOCATION_PROVIDER • LocationManager • LocationProvider • LocationListener
LocationManager LocationManagerlocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); List<String> providersAll = locationManager.getAllProviders(); List<String> providersEnabled= locationManager.getProviders(true); LocationlastKnownLocation = locationManager.getLastKnownLocation(String provider); doublegetLatitude() double getLongitude() if(hasAltitude()) getAltitude() floatdistanceTo(Location dest) String getProvider() Location
Criterias String getBestProvider(Criteria criteria, booleanenabledOnly) Criteriacriteria = newCriteria();
LocationProviders of 24 LocationProvidergetProvider(String name) booleanhasMonetaryCost() booleanrequiresCell() booleanrequiresNetwork() booleanrequiresSatellite() booleansupportsAltitude() booleansupportsBearing() booleansupportsSpeed()
LocationListener • newLocationListener() { • @Override • public voidonStatusChanged(String provider, int status, Bundle extras) {} • @Override • public voidonProviderEnabled(String provider) {} • @Override • public voidonProviderDisabled(String provider) {} • @Override • public voidonLocationChanged(Locationlocation) {} • } void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
Using Emulator and DDMS > telnet localhost 5554 > geo fix -122.08409637.422005
GeoCoding privatevoiddoInBackground() { List<Address> foundGeocode; doublelatitude; doublelongitude; try { foundGeocode = newGeocoder(this).getFromLocationName("ADDRESS", N); for(int n = 0; n < N; n++) { latitude= foundGeocode.get(n).getLatitude(); longitude= foundGeocode.get(n).getLongitude(); } } catch(IOException) { // Log.e(...); } }
Google Maps Android v1 API Note: Version 1 of the Google Maps Android API has been officially deprecated as of December 3rd, 2012. This means that from March 18th, 2013 you will no longer be able to request an API key for this version. No new features will be added to Google Maps Android API v1. However, apps using v1 will continue to work on devices. Existing and new developers are encouraged to use Google Maps Android API v2. (Deprecated?)
private void setUpMap() { mapView = (MapView) findViewById(R.id.mapView); mapView.setBuiltInZoomControls(true); mapView.setOnSingleTapListener(new OnSingleTapListener() { @Override public booleanonSingleTap(MotionEvent e) {} }); mapOverlays = mapView.getOverlays(); myLocationOverlay = new MyLocationOverlay(this, mapView); myLocationOverlay.enableMyLocation(); myLocationOverlay.enableCompass(); myLocationOverlay.runOnFirstFix(new Runnable() { @Override public void run() { MapControllermapController = mapView.getController(); mapController.animateTo(currentLocation); mapController.setZoom(17); mapView.getOverlays().add(myLocationOverlay); } }); mapOverlays.add(myLocationOverlay); Google MapsAndroid v1 API
// Construct a CameraPosition focusing on PWr // and animate the camera to that position. • CameraPositioncameraPosition = new CameraPosition.Builder() .target(newLatLng(51.107359,17.059974)) // Sets the center of the map to C-13 .zoom(17) // Sets the zoom .bearing(90) // Sets the orientation of the camera to east .tilt(30) // Sets the tilt of the camera to 30 degrees .build(); // Creates a CameraPosition from the buildermap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); • Weaknesses: • No Android Emulator suport! • Poordocumented… • Requirespowerfulldevice to display 3D view Google Maps Android API v2
Efficency Summary • Networking data • Battery saving It would be quite nice to remember and takecareabout: