• About Us
  • Classroom Training
  • Online Training
    • Self Paced Courses
      • Android Development for Beginners
      • HTML and CSS For Beginners (with HTML5)!
      • Javascript for Beginners
      • C Programming: iOS Development Starts Here!
      • Objective C For Beginners
      • PHP MySQL For Beginners
      • Web Development Code Camp
    • Live Instructor Courses
      • HTML and CSS with HTML 5 (Live)
      • Javascript for Beginners (Live)
      • C Programming for Beginners (Live)
      • Objective C for Beginners(Live)
      • Android Development Code Camp (Live)
      • iOS Development Code Camp (Live)
      • HTML5 & Advanced Client Side Development (Live)
      • Actionscript for Beginners (Live)
  • Forum
  • Testimonials
  • Contact Us
    • Actionscript
    • Android
    • C/C++/Objective C
    • HTML/CSS
    • iOS
    • Java
    • Javascript
    • PHP
Home > Android > Android LocationManager Tutorial

Android LocationManager Tutorial

Posted by Mark Lassoff on August 7th, 2011 | 3 Comments

In this Android LocationManager tutorial, Mark shows you how to create a location aware Android application. The application that you will create uses a GPS Provider to find the user’s current location and displays the latitude and longitude on the unit. As the user moves the Android LocationManager senses a new location. The LocationListener interface is used to code events that occur as the location changes, the location is unavailable or the location is established.

This Android LocationManager tutorial is designed to be a gentle introduction to the LocationManager class in Android. The LocationManager actually provides more information than simple latitude and longitude. For complete documentation the LocationManager see the Google Developers’ Reference.

[box type="warning"] A common mistake when attempting to complete this tutorial is forgetting to edit the Manifest.xml file. The fine location permission must be added to the manifest in order for this app to function. See the video for further information [/box]

main.xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Latitude" android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceSmall"></TextView>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textLat"></TextView>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Longitude" android:id="@+id/textView3" android:textAppearance="?android:attr/textAppearanceSmall"></TextView>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textLong"></TextView>
</LinearLayout>

MGeolocationActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.learntoprogram.android;
 
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
 
public class MGeolocationActivity extends Activity {
 
	TextView textLat;
	TextView textLong;
 
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        textLat = (TextView)findViewById(R.id.textLat);
        textLong = (TextView)findViewById(R.id.textLong);
 
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    }
        class mylocationlistener implements LocationListener{
 
			@Override
			public void onLocationChanged(Location location) {
				if(location != null)
				{
					double pLong = location.getLongitude();
					double pLat = location.getLatitude();
 
					textLat.setText(Double.toString(pLat));
					textLong.setText(Double.toString(pLong));
 
				}
 
			}
 
 
			@Override
			public void onProviderDisabled(String provider) {
 
			}
 
			@Override
			public void onProviderEnabled(String provider) {
 
			}
 
			@Override
			public void onStatusChanged(String provider, int status,
					Bundle extras) {
 
			}
 
        }
    }

3 Responses

  1. zia says:
    December 5, 2011 at 4:26 am

    hello sir …
    i m very happy to see the tutorial butt its not work for me .when i execute the code. emulator close the apps force close error .please help me please make more like this.
    and if u don’t mind please mail me at zia_shahid50@yahoo.com

    Reply
    • Mark Lassoff says:
      December 5, 2011 at 12:14 pm

      Can you send me your code as a text file so I can have a look? mark at learntoprogram.tv.

      Reply
  2. Max says:
    February 28, 2012 at 2:32 am

    This helped me so much.

    Thank you

    Reply

Leave a Reply

Click here to cancel reply.

Join our mailing list

Free video tutorials, course information and more!

* required

Email Address *

First Name

Last Name:

Email Marketing Software by VerticalResponse
Apple Store Facebook Google+ LinkedIn RSS Feed Twitter YouTube
Provided by The Social Links

Like Us!

Live Online Course

Online event registration powered by Eventbrite

Self Paced Course

What is ?

We train software, web and mobile developers. Our classes geared to the way adults learn are available online, or in-person at your office or training center.

New Android Book

Android Programming Code Camp by Mark Lassoff. Coming in 2012. Finally, a true beginners book on Android programming and development. No Experience required.

Email me when the book is available

On Learning

  • Top Languages Used in Web Development
  • How To Learn a Programming Language: 5 Tips
©LearnToProgram.tv 2012 .All rights reserved.