introducing to location in android

18
Introducing to Location in Android LOCATION IS EVERYTHING Kamil Lelonek [email protected]

Upload: eudora

Post on 14-Feb-2016

18 views

Category:

Documents


0 download

DESCRIPTION

Introducing to Location in Android. Location is everything. Kamil Lelonek [email protected]. What we gonna talk about ?. Introduction to GEOgraphy How we „ describe ” the Earth? How we calculate coordinates ? Important issues to avoid problems - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introducing  to  Location  in Android

Introducing to Location in Android

LOCATIONIS EVERYTHING

Kamil [email protected]

Page 2: Introducing  to  Location  in Android

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

Page 3: Introducing  to  Location  in Android

Let’s go back to school

º - degrees’ - minutes’’ - seconds

East (E)West (W)(N)

(S)

Page 4: Introducing  to  Location  in Android

How to calculate it?23º25’24’’ S

23º25’24’’ S → – 23,439167 (double)(int) = 1E6 * (double) → – 23439167 (int)

Page 5: Introducing  to  Location  in Android

Attention Problems Extras

- Positive and Negative values

- Latutude goes firstCalcuation problems

- Altitude- Azimuth- Speed

Page 6: Introducing  to  Location  in Android

Get prepared

Permissionso ACCESS_MOCK_LOCATION - emulatoro ACCESS_COARSE_LOCATION - approximateo ACCESS_FINE_LOCATION - preciseo ACCESS_LOCATION_EXTRA_COMMANDS o INSTALL_LOCATION_PROVIDER

LocationManager

LocationProvider

LocationListener

Page 7: Introducing  to  Location  in Android

LocationManager

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

List<String> providersAll = locationManager.getAllProviders();

List<String> providersEnabled = locationManager.getProviders(true);

Location lastKnownLocation = locationManager.getLastKnownLocation(String provider);

Location

double getLatitude()

double getLongitude()

if(hasAltitude()) getAltitude()

float distanceTo(Location dest)

String getProvider()

Page 8: Introducing  to  Location  in Android

Criterias

String getBestProvider(Criteria criteria, boolean enabledOnly)

Criteria criteria = new Criteria();

int ACCURACY_COARSint ACCURACY_FINEint ACCURACY_HIGHint ACCURACY_LOWint ACCURACY_MEDIUMint NO_REQUIREMENTint POWER_HIGHint POWER_LOWint 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)

Page 9: Introducing  to  Location  in Android

LocationProviders

LocationProvider getProvider(String name)

boolean hasMonetaryCost()

boolean requiresCell()

boolean requiresNetwork()

boolean requiresSatellite()

boolean supportsAltitude()

boolean supportsBearing()

boolean supportsSpeed()

of 24

Page 10: Introducing  to  Location  in Android

LocationListener

new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) {}

@Override public void onProviderEnabled(String provider) {}

@Override public void onProviderDisabled(String provider) {}

@Override public void onLocationChanged(Location location) {} }

void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)

Page 11: Introducing  to  Location  in Android

Using Emulator and DDMS

> telnet localhost 5554 > geo fix -122.084096 37.422005

Page 12: Introducing  to  Location  in Android

GeoCodingprivate void doInBackground() {

List<Address> 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(...);}

}

Page 13: Introducing  to  Location  in Android

Google Maps Android v1 API (Deprecated?)

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.

Page 14: Introducing  to  Location  in Android

Google Maps Android v1 API

private void setUpMap() {mapView = (MapView) findViewById(R.id.mapView);

mapView.setBuiltInZoomControls(true); mapView.setOnSingleTapListener(new OnSingleTapListener() {

@Override public boolean onSingleTap(MotionEvent e) {} });

mapOverlays = mapView.getOverlays();myLocationOverlay = new MyLocationOverlay(this, mapView);

myLocationOverlay.enableMyLocation();myLocationOverlay.enableCompass(); myLocationOverlay.runOnFirstFix(new Runnable() {

@Override public void run() {MapController mapController =

mapView.getController();mapController.animateTo(currentLocation);mapController.setZoom(17);mapView.getOverlays().add(myLocationOverlay);

}});mapOverlays.add(myLocationOverlay);

Page 15: Introducing  to  Location  in Android

Google Maps Android API v2

Page 16: Introducing  to  Location  in Android

Google Maps Android API v2

Page 17: Introducing  to  Location  in Android

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(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));

Google Maps Android API v2

Page 18: Introducing  to  Location  in Android

SummaryIt would be quite nice to remember and take care about:

Efficency

Battery saving

Networking data