Android GPS Using. How to get current location example

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
Tags: ,

24 thoughts on “Android GPS Using. How to get current location example

  • By Steve - Reply

    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. :)

    • By daniyal - Reply

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

    • By daniyal - Reply

      have you completed it ? can u provide source code ?

  • By Nguyen Yen - Reply

    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

  • By Igor - Reply

    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!

  • By Nguyen Yen - Reply

    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.

  • By Igor - Reply

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

  • By Nguyen Yen - Reply

    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!

  • By Igor - Reply

    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.

  • By Péter B. - Reply

    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

  • By Igor - Reply

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

  • By Nguyen Yen - Reply

    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?

  • By irem - Reply

    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..

    • By Ray - Reply

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

    • By saranya - Reply

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

      thank you sir.

      • By Igor Khrupin - Reply

        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

  • By d.s - Reply

    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?

  • By ThuHuong - Reply

    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 ?

  • By ikarus - Reply

    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

  • By Rajesh - Reply

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

  • By Engin - Reply

    Hi

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

  • By simran - Reply

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

  • By Igor - Reply

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

  • By Niraj - Reply

    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.

Leave a Reply

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


+ 8 = ten

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">