introducing to location in android

Post on 14-Feb-2016

28 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Introducing to Location in Android

LOCATIONIS EVERYTHING

Kamil LelonekKamil.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

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,439167 (double)(int) = 1E6 * (double) → – 23439167 (int)

Attention Problems Extras

- Positive and Negative values

- Latutude goes firstCalcuation problems

- Altitude- Azimuth- Speed

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

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

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)

LocationProviders

LocationProvider getProvider(String name)

boolean hasMonetaryCost()

boolean requiresCell()

boolean requiresNetwork()

boolean requiresSatellite()

boolean supportsAltitude()

boolean supportsBearing()

boolean supportsSpeed()

of 24

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)

Using Emulator and DDMS

> telnet localhost 5554 > geo fix -122.084096 37.422005

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

}

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.

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

Google Maps Android API v2

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

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

Efficency

Battery saving

Networking data

top related