7

Possible Duplicate:
How do I get the current GPS location programmatically in Android?

I have an application in which I want to get the current location.

also I want to get the latitude and longitude so I store that and return back that position.

how can do it...

Community
  • 1
  • 1
pratik
  • 983
  • 3
  • 13
  • 25
  • 1
    why don't you search for similar in StackOverflow before asking the question? – MKJParekh Mar 01 '12 at 04:49
  • @Soni:,@Peter O.: Sorry for asking duplicate question here. But all code were giving me blank screen, so i thought it is not workinng. But actually it was taking too much time to find location. I will be careful next time..!! – pratik Mar 01 '12 at 05:14

1 Answers1

6

try following code segment,

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

public class LocListener implements LocationListener
{
    private static double lat =0.0;
    private static double lon = 0.0;
    private static double alt = 0.0; 
    private static double speed = 0.0;

    public static double getLat()
    {
        return lat;
    }

    public static double getLon() 
    {
        return lon;
    }

    public static double getAlt()
    {
        return alt;
    }

    public static double getSpeed()
    {
        return speed;
    }

    @Override
    public void onLocationChanged(Location location) 
    {
        lat = location.getLatitude();
        lon = location.getLongitude();
        alt = location.getAltitude();
        speed = location.getSpeed(); 
    }

    @Override
    public void onProviderDisabled(String provider) {}
    @Override
    public void onProviderEnabled(String provider) {}
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • :but it gives me a force close........ – pratik Mar 01 '12 at 05:01
  • but same lines working excellent in my product. – Lucifer Mar 01 '12 at 05:03
  • :another example i have tries it is working properly but it takes much time to finding current location. and another thing i marked that i am at a fix location but "onLocationChanged()" method is being called many times... can you tell me reason for that? – pratik Mar 01 '12 at 05:08
  • 2
    your physical location can be fix, but the returned values from satelite will be differ every time because of accuracy in meters. this is the reason onLocationChanged() method will call every time. – Lucifer Mar 01 '12 at 05:27
  • good to hear that, for tracking location, your device needs to be in open air or near window. – Lucifer Mar 01 '12 at 05:32
  • I have accepted your answer but i used Vaibhav`s link.and that worked for me very well... – pratik Mar 01 '12 at 05:42
  • well both are same, mine code is optimized format of that code. – Lucifer Mar 01 '12 at 05:45