Hi. Here i want show you how to use Android GPS quickly. This is simple, may be not perfect example how to get current location by GPS.
To use GPS in your application first of all you must specify the uses-permission in Android manifest file:

If you don’t set this permission the application can’t get access to location service.

To use this application with android emulator you must mock location data. To do this using Eclipse you must:

  • Select Window > Show View > Other > Emulator Control
  • In Emulator Control panel enter the GPS coordinates under Location Controls and press Send

This operation you must do every time then you Run application.

Below you can see the application activity (HelloAndroidGpsActivity.java) source. Below you can download the source code of this Android project.
For more information about Android GPS Usage look HERE

Here you can download the source code of this Android project.

Download it from github

37 Comments

Steve · 28 July, 2011 at 10:08

Thanks, this was useful. Now all I have to do is send the location to the server and retrieve all the database entries within 10 miles. Simples. 🙂

    daniyal · 1 July, 2014 at 12:03

    have you done it ?? can u provide it ??

    daniyal · 1 July, 2014 at 12:03

    have you completed it ? can u provide source code ?

    nassima · 21 March, 2017 at 00:58

    how to send the location to the server

Nguyen Yen · 5 October, 2011 at 09:03

Thanks for share, but when i use your program, my phone don’t get location. In textview display: Sorry, location is not determined
My phone turned on GPS, enable data (3G). I don’t unsderstand, location is null?
If you know, plase help me
Thanks

Igor · 5 October, 2011 at 10:08

Hi, Nguyen Yen
I just updated my post and source code. In past I used only GPS provider.
Please look at updates. Hope it help you!

Nguyen Yen · 5 October, 2011 at 10:33

Hi Igor Khrupin!
when i run your new project. I submit ‘Get My Location’, program progress about 5 minutes, after textview was null. My phone is HTC hero, if i try with emulator, result is ok.
Thank you very much.

Igor · 5 October, 2011 at 10:38

You can’t use GPS provider when you indoors. Please enable Network provider.

Nguyen Yen · 6 October, 2011 at 03:14

I tested outdoors yesterday, result wasn’t ok. I turned on GPS: Settings -> location &security -> use GPS satellites (checked).
Network provider: Settings -> wireless & network settings -> mobile network -> data enabled(checked).
can you see my wrong?
Thanks!

Igor · 6 October, 2011 at 08:52

About Network Location provider.
You wrote: “Network provider: Settings -> wireless & network settings -> mobile network -> data enabled(checked).”

You enabled access to internet via your mobile operator.

Please make checkbox checked in Settings -> Location -> Use wireless networks. It is settings in my HTC Desire. I don’t know what name of this setting in your HTC Hero. This setting means that you can use a-GPS.

Péter B. · 3 November, 2011 at 00:01

Hi!

Your tutorial is very useful, but it doesn’t work for mee!
I am using emulator 2.1 can it be a problem?

Thanks,
Peter

Igor · 10 November, 2011 at 09:58

Hi, Peter!
Did you mock location in your emulator before running the app?

Nguyen Yen · 11 November, 2011 at 04:24

hi Igor
when i used your program. i saw in logcat:
09-21 04:34:21.004: DEBUG/libloc(189): Event RPC_LOC_EVENT_PARSED_POSITION_REPORT (client 0)
09-21 04:34:21.004: DEBUG/libloc(189): Session status: RPC_LOC_SESS_STATUS_IN_PROGESS Valid mask: 0x606D
09-21 04:34:21.004: DEBUG/libloc(189): Latitude: 15.8900017 (intermediate)
09-21 04:34:21.004: DEBUG/libloc(189): Longitude: 105.8000028
09-21 04:34:21.004: DEBUG/libloc(189): Accuracy: 0.0000000
09-21 04:34:21.364: DEBUG/libloc(189): Event RPC_LOC_EVENT_SATELLITE_REPORT (client 0)
09-21 04:34:21.364: DEBUG/libloc(189): sv count: 0
but in textview: “My Location is”: process (not display longitude and latitude)..
Can you help me get infomation in log?

irem · 7 January, 2012 at 14:22

Hi, thanks for this example..
I got latitude and longitude datas correctly.. Bu t I have a problem. I would like to get also speed (car speed). For this aim, I used :
” String speed = “Speed: ” + location.getSpeed(); ” And, I tested program with my telephone in the car that in motion. Hovewer, I got speed values like 1.39466, 1.69499 or 0. I don’t understand this problem reason.

I hope, you will help me..

    Ray · 31 March, 2014 at 20:11

    The metric is meters per second so the value you got would be correct

    saranya · 14 October, 2014 at 07:21

    sir when you got that answer for your car speed. what is actual speed of the car in kilimeter per hour?..

    thank you sir.

      Igor Khrupin · 15 October, 2014 at 13:55

      Hi Saranya,

      Speed will be in meters per second.
      From API: “Get the speed if it is available, in meters/second over ground.”

      1 meter per second = 3,6 kilometre per hour

    zcxvZ · 27 June, 2017 at 10:11

    yes wrong

d.s · 22 January, 2012 at 07:52

I get an error here

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editTextShowLocation = (EditText) findViewById(R.id.editTextShowLocation);

progress = (ProgressBar) findViewById(R.id.progressBar1);
progress.setVisibility(View.GONE);

buttonGetLocation = (Button) findViewById(R.id.buttonGetLocation);
buttonGetLocation.setOnClickListener(this);

locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}

It says id can’t be resolved or is not a field.What to do?

ThuHuong · 9 February, 2012 at 10:56

Hi, this tutorial is very useful.
I have a question.
If my device enable GPS and network then :
– duplicate call get location ?
– get location will run slowly ?

ikarus · 23 February, 2012 at 19:16

hi..thx for your psot if very helpfull. btw i try to adding the geocode into your code for get a adress form the lat and long with a litle modification, but i got an force close.. can you help me

Rajesh · 21 March, 2012 at 11:51

This tutorial is very useful.Great…….!!!!!!!!!!!!!!!!

Engin · 14 April, 2012 at 18:55

Hi

Why are we sending mock location data ? Is this code get direction when i push the “Get My Direction” ?

simran · 28 June, 2012 at 11:11

hi,
i want to save current coordinates in sqlite. how can i save them???

    Shubham · 21 March, 2016 at 16:06

    hey are you done storing the location in database ?
    can you help me ?

    thanks in advance

      Igor Khrupin · 22 April, 2016 at 11:11

      Hi, current sample don’t save location in the DB.
      How can I help you?

Igor · 28 June, 2012 at 23:21

Hi, simran
You can create table with two columns “lat” and “lng”. Then you can put coordinates into these columns as TEXT data.

Niraj · 6 August, 2012 at 12:53

I am using this tutorial for finding GPS locaton and my code is as same as above but i am not geting location. only this message ” Sorry, location is not determined” is displayed. Please help me.

suzan maka · 17 June, 2016 at 13:48

It is a nice tutorial. Thanks man.

Example · 26 August, 2016 at 11:13

how to get location without turning on gps and internet

Example · 10 September, 2016 at 10:31

Have already used FusedLocationProviderApi, but its still demand Kindly turn on the gps
if i turn on the gps it will capture network location
if possible can u please share working code to take location without turning on gps

vitorcrdias · 9 November, 2016 at 21:40

This code is Great, but doesn’t meet the needs of somebody which has to keep running in background… 🙁

nowrin · 6 March, 2017 at 11:56

hello! i need an android code for location based task reminder..can anyone help plz?

Leave a Reply

Your email address will not be published. Required fields are marked *