In this article, we will create an Android application which displays latitude and longitude of the current location using GPS and Android’s LocationManager API.
An extension to this application is available in the article titled “Showing current location in Google Maps with GPS and LocationManager in Android“, where current location is displayed in Google Map.
This application is developed in Eclipse ( 4.2.0 ) with ADT plugin ( 20.0.3 ) and Android SDK ( R20.0.3 ) .
1. Create a new Android application project namely “LocationFromGPS”
2. Design application launcher icon
3. Create a blank activity to define the class MainActivity
4. Enter MainActivity details
5. Delete Android’s backward compatibility support library from the project, if exists
By default Eclipse ( 4.2.0) adds Android Support Library to Android application project. For this application, we don’t need to use this support library. So the library file libs/android-support-v4.jar may be removed manually via ProjectExplorer by simply right click on the file and then clicking the menu item “delete”.
6. Update the file res/values/strings.xml
<resources> <string name="app_name">LocationFromGPS</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">Location From GPS</string> <string name="str_tv_location">Current Location</string> </resources>
7. Update the file res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="@string/str_tv_location" android:textStyle="bold" /> <TextView android:id="@+id/tv_longitude" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_location" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/tv_latitude" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_longitude" android:layout_centerHorizontal="true"/> </RelativeLayout>
8. Update the file res/values/styles.xml
<resources> <style name="AppTheme" parent="android:Theme" /> </resources>
9. Update the file res/values-v11/styles.xml
<resources> <style name="AppTheme" parent="android:Theme.Holo" /> </resources>
10. Update the file res/values-v14/styles.xml
<resources> <style name="AppTheme" parent="android:Theme.Holo" /> </resources>
11. Update the class MainActivity in the file src/in/wptrafficanalyzer/locationfromgps/MainActivitiy.java
package in.wptrafficanalyzer.locationfromgps; 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.view.Menu; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements LocationListener{ LocationManager locationManager ; String provider; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Getting LocationManager object locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); // Creating an empty criteria object Criteria criteria = new Criteria(); // Getting the name of the provider that meets the criteria provider = locationManager.getBestProvider(criteria, false); if(provider!=null && !provider.equals("")){ // Get the location from the given provider Location location = locationManager.getLastKnownLocation(provider); locationManager.requestLocationUpdates(provider, 20000, 1, this); if(location!=null) onLocationChanged(location); else Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onLocationChanged(Location location) { // Getting reference to TextView tv_longitude TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude); // Getting reference to TextView tv_latitude TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude); // Setting Current Longitude tvLongitude.setText("Longitude:" + location.getLongitude()); // Setting Current Latitude tvLatitude.setText("Latitude:" + location.getLatitude() ); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }
12. Update the file AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.wptrafficanalyzer.locationfromgps" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
13. Enable GPS in the emulator or device from Settings
14. Set Longitude and Latitude of the location in Eclipse -> DDMS ( Perspective ) to run this application in the emulator
15. Screenshot of the application
16. Download Source Code

17. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com
I am getting multiple markers and overide error in mainactivity.java
please reply me soon
Which method is showing override error?
public void on location changed method then got error..
how to increase these current location values upto 10 showing in a list
Sorry, i am not getting you.
it was urgent
Wow It works thanx george
when i load it in a blackberry 10 device it cannot access gps and return my location
Thank you very much!!! It works excellently!!! It is the only project that works on my phone!!!!!!!!!!))))))))))))
Can this latitude and longitude change with movement of device?
Can we change the value of latitude and longitude with this code where we are roaming?
Thanksssss buddy
Thanks for the tutorial, I’m gonna follow the whole series!
Cheers!!!
please say me, is it necessary to send values from the emulator control
Can it be possible to save it on some server from where other can get it as real time navigation
please help its showing me my old location which is last updated previously on my phone too far from my current location..
how can i use latitude and longitude values or pass lat/long values in broadcast receiver class. It would be gr8 if u help me.
Hi George,
Could you help GPS app creation by use of JNI. My hardware uses UART GPS, for that wrote c application, works fine and gives correct latitude, longitude, UTC time, Position and Data validation.
I have to create jni for GPS app can you help me how it is possible.
Thanks,
vs
hi,
please help me to create a location based application.The app is for changing the profile(silent/normal) based on the location.
plz help me..
how can i get the city name ?
Dear
Mr . George Mathew
Please Help
Can I get a mobile location by using IMEI number
Thanks,
hi sir i got the lat and long current thk u for tutorial.
I am doing a app in that i need to show the current location throught google map and to point some places in it ,can u help me. i didn’t got.
I have a question? does it work just outdoor? it doesn’t work in home?
i want to a code for getting current location automatically in gps and displaying destination with total distance. n should display the shortest distance
hi after getting lat and long value,how to hide gps traking icon ?
Hi George Mathew,
Please Tell me How to get a friends location latitude and longitude
Hi George thanks a lot for this tutorial
i want to track current location of other ppl that are on my contact list..
how to do it .. plz rply ???
hello manvi
if get such type of code then please also forword on my email :- meyogeshvalecha@gmail.com
Even me, i would like to know how to track current location of other ppl. Plz let me know if u guys got the code.
Hello George Mathew,
I have more than 10 location with lat long but how to compare nearest location with my location, Please help me…
Hi,
I have resolved all problems.your code is very helpful for me,thnx a lot.
sir, how can i store these credentials into database or into a text file?
Hai Sir I need to Know How to Save the Latitude and longitude values in sqlite in android
plz help me as soon as possible
hi i m ravi and i want to creatr a gps system to get location
hi!! your tutorial is very helpful for me. i need some clarification the value of longitude and latitude is always retrieving false value.
This application worked great in Android Studio, with one exception: the menu.activity_main, I renamed to the default ‘menu_main.
How to display google map on android application?
would you mind to help me developing sms location where we can send the location viz sms?
Actually GPS positioning works fine, but NETWORK positioning doesn’t. When I have turned devices GPS on coordinates keep changing while i am moving, but doesn’t happen same when I have turn it off and relay on the NETWORK_PROVIDER..!!!
hai im working on project for finding alternative routes for same destination could you please help me with this project i would like to know if i can get a source code regarding the project
hy dear I copy your code as it but not receive any value in TxtView
I want to save the location and display it in application later.