Professional Documents
Culture Documents
Lars Vogel
Version 4.1 Copyright 2010 , 2011, 2012 Lars Vogel 20.08.2012
Revision History
Revision 0.1
15.06.2010
Lars Vogel
created
18.06.2010 - 20.08.2012
Lars Vogel
Android Location API This tutorial describes the usage of the Android Location API. It is based on Eclipse 3.7, Java 1.6 and Android 4.0 (Ice Cream Sandwich).
Table of Contents
1. Android Basics 2. Android Location API 2.1. Determine the current geolocation 2.2. LocationManager 2.3. LocationProvider 2.4. Selecting LocationProvider via Criteria 2.5. Proximity Alert 2.6. Forward and reverse Geocoding
2.7. Security 2.8. Prompt the user to Enabled GPS 3. Using GPS and setting the current location 3.1. Activating GPS on the emulator 3.2. Setting the geoposition 4. Tutorial: Using the Android Location API 4.1. Create Project 4.2. Add permissions 4.3. Activity 4.4. Run and Test 5. Thank you 6. Questions and Discussion 7. Links and Literature 7.1. Source Code 7.2. Android Resources 7.3. vogella Resources
1. Android Basics
The following assumes that you have already basic knowledge in Android development. Please check the Android development tutorial for the basics.
2.2. LocationManager
The LocationManager class provides access to the Android location service. This services allows to access location providers, to register location update listeners and proximity alerts and more.
2.3. LocationProvider
The LocationProvider class is the superclass of the different location providers which deliver the information about the current location. This information is stored in the Location class.
The Android device might have several LocationProvider available and you can select which one you want to use. In most cases you have the followng LocationProvider available. Table 1. LocationProvider
LocationProvider Description
network
Uses the mobile network or WI-Fi to determine the best location. Might have a higher precision in closed rooms then GPS.
gps
Use the GPS receiver in the Android device to determine the best location via satellites. Usually better precision than network.
passive
2.7. Security
If you want to access the GPS sensor, you need the ACCESS_FINE_LOCATION permission. Otherwise you need the ACCESS_COARSE_LOCATION permission.
// Check if enabled and if not send user to the GSP settings // Better solution would be to display a dialog and suggesting to // go to the settings
if (!enabled) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); }
Typically you would open an AlarmDialog prompt the user and if he wants to enable GPS or if the application should be canceled. You cannot enable the GPS directly in your code, the user has to do this.
You need to activate GPS on your test device. If you test on the emulator and its not activated you "null" if you try to use a LocationManager. The Google Map Activity should automatically activate the GPS device in the emulator but if you want to use the location manager directly you need to do this yourself. Currently their seems to be an issue with this. Start Google Maps on the emulator and request the current geo-position, this will allow you to activate the GPS. Send new GPS coordinates to the Android emulator.
You can also set the geoposition the Android emulator via telnet. Open a console and connect to your device. The port number of your device can be seen in the title area of your emulator.
telnet localhost 5554
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="5dip" android:text="Latitude: " android:textSize="20dip" >
</TextView>
<TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_marginRight="5dip" android:text="Longitute: " android:textSize="20dip" > </TextView>
</LinearLayout>
</LinearLayout>
4.3. Activity
Change ShowLocationActivity to the following. It queries the location manager and display the queried values in the activity.
package de.vogella.android.locationsapi.simple;
import android.app.Activity; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast;
public class ShowLocationActivity extends Activity implements LocationListener { private TextView latituteField; private TextView longitudeField; private LocationManager locationManager; private String provider;
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); latituteField = (TextView) findViewById(R.id.TextView02); longitudeField = (TextView) findViewById(R.id.TextView04);
// Define the criteria how to select the locatioin provider -> use // default
Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(provider);
@Override
public void onLocationChanged(Location location) { int lat = (int) (location.getLatitude()); int lng = (int) (location.getLongitude()); latituteField.setText(String.valueOf(lat)); longitudeField.setText(String.valueOf(lng)); }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
@Override
public void onProviderEnabled(String provider) { Toast.makeText(this, "Enabled new provider " + provider, Toast.LENGTH_SHORT).show();
@Override
public void onProviderDisabled(String provider) {