Introducing to Location in Android LOCATION IS EVERYTHING Kamil Lelonek Kamil Lelonek
What we gonna talk about? Introduction to GEOgraphy How we „describe” the Earth? How we calculate coordinates? Important issues to avoid problems Finding user location Prepare to coding Ways to obtain current position This time emulator is helpful Developing Maps application GeoCoding Good old Google Maps API v1 New look and feel Google Maps API v2 Summary Navigation PRO tips for advanced users Q&A
Let’s go back to school º - degrees ’ - minutes ’’ - seconds East (E)West (W) (N) (S)
How to calculate it? 23º25’24’’ S 23º25’24’’ S→– 23, (double) 23º25’24’’ S→– 23, (double) (int) = 1E6 * (double) → – (int)
Attention Problems Extras
Get prepared Permissions oACCESS_MOCK_LOCATION - emulator oACCESS_COARSE_LOCATION - approximate oACCESS_FINE_LOCATION - precise oACCESS_LOCATION_EXTRA_COMMANDS oINSTALL_LOCATION_PROVIDER LocationManager LocationProvider LocationListener
LocationManager LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); List providersAll = locationManager.getAllProviders(); List providersEnabled = locationManager.getProviders(true); Location lastKnownLocation = locationManager.getLastKnownLocation(String provider); Location double getLatitude() double getLongitude() if(hasAltitude()) getAltitude() float distanceTo(Location dest) String getProvider()
Criterias String getBestProvider(Criteria criteria, boolean enabledOnly) Criteria criteria = new Criteria(); int ACCURACY_COARS int ACCURACY_FINE int ACCURACY_HIGH int ACCURACY_LOW int ACCURACY_MEDIUM int NO_REQUIREMENT int POWER_HIGH int POWER_LOW int POWER_MEDIUM void setAccuracy(int accuracy) void setAltitudeRequired(boolean altitudeRequired) void setBearingAccuracy(int accuracy) void setBearingRequired(boolean bearingRequired) void setCostAllowed(boolean costAllowed) void setHorizontalAccuracy(int accuracy) void setPowerRequirement(int level) void setSpeedAccuracy(int accuracy) void setSpeedRequired(boolean speedRequired) void setVerticalAccuracy(int accuracy)
LocationProviders LocationProvider getProvider(String name) boolean hasMonetaryCost() boolean requiresCell() boolean requiresNetwork() boolean requiresSatellite() boolean supportsAltitude() boolean supportsBearing() boolean supportsSpeed() of 24
LocationListener new LocationListener() public void onStatusChanged(String provider, int status, Bundle extras) public void onProviderEnabled(String provider) public void onProviderDisabled(String provider) public void onLocationChanged(Location location) {} } new LocationListener() public void onStatusChanged(String provider, int status, Bundle extras) public void onProviderEnabled(String provider) public void onProviderDisabled(String provider) public void onLocationChanged(Location location) {} } void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
Using Emulator and DDMS > telnet localhost 5554 > geo fix
GeoCoding private void doInBackground() { List foundGeocode; double latitude; double longitude; try { foundGeocode = new Geocoder(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 (Deprecated?) Note: Version 1 of the Google Maps Android API has been officially deprecated as of December 3rd, 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.Google Maps Android API v2
Google Maps Android v1 API private void setUpMap() { mapView = (MapView) findViewById(R.id.mapView); mapView.setBuiltInZoomControls(true); mapView.setOnSingleTapListener(new OnSingleTapListener() public boolean onSingleTap(MotionEvent e) {} }); mapOverlays = mapView.getOverlays(); myLocationOverlay = new MyLocationOverlay(this, mapView); myLocationOverlay.enableMyLocation(); myLocationOverlay.enableCompass(); myLocationOverlay.runOnFirstFix(new Runnable() public void run() { MapController mapController = mapView.getController(); mapController.animateTo(currentLocation); mapController.setZoom(17); mapView.getOverlays().add(myLocationOverlay); } }); mapOverlays.add(myLocationOverlay);
Google Maps Android API v2
Weaknesses: No Android Emulator suport! Poor documented… Requires powerfull device to display 3D view // Construct a CameraPosition focusing on PWr // and animate the camera to that position. CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng( , )) // 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 builder map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); Google Maps Android API v2
Summary It would be quite nice to remember and take care about: Efficency Battery saving Networking data