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) { } } } |



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
Can you send me your code as a text file so I can have a look? mark at learntoprogram.tv.
This helped me so much.
Thank you