Introducing to Location in Android
LOCATION
IS EVERYTHING
Kamil Lelonek
Kamil.Lelonek@student.pwr.wroc.pl
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
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
Attention Problems Problems Extras Extras
- Positive and Negative values
- Latutude goes first
Calcuation problems
- Altitude
- Azimuth
- Speed
Get prepared
Permissions
o ACCESS_MOCK_LOCATION - emulator o ACCESS_COARSE_LOCATION - approximate o ACCESS_FINE_LOCATION - precise
o 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 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
LocationListene r
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) {}
}
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
GeoCoding
private 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);