0

I am making an app in which i have to get latitude and longitude of device and my code is as follows:

mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3600, 0,  mlocListener);
    System.out.println("mlocManager"+mlocManager);
    String str = latitude + "," + longitude ;
    System.out.println("latitude"+latitude);
    System.out.println("longi:"+longitude);

}

public class MyLocationListener implements LocationListener
 {
    public void onLocationChanged(Location loc)
    {
        try 
        {
        System.out.println("............ ..............................Location changedin 11");
        latitude = loc.getLatitude();

        longitude = loc.getLongitude();
               // System.out.println("latitude"+curr_lat);

        System.out.println("longitude curr_lon");
        loc.getAccuracy();

                    }
        catch (Exception e1) {
            e1.printStackTrace();
        }

        }

    @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

    }
    }

But at the end i am getting lat and long as 0.0 . Can anyone help me.

1 Answers1

0

Add the following permissions

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

You should give some time to get location info like 10sec,20sec etc.For this you can use timer.

I have given an example.You can Implement like this.

private Location getCurrentLocation(){
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
        t.cancel();
        mLatitude = location.getLatitude();
        mLongitude =  location.getLongitude();
        myGeoPoint = GeoTools.makeGeoPoint(mLatitude, mLongitude);
        mapController.animateTo(myGeoPoint);

    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation;
Timer t = new Timer();

    t.schedule(new TimerTask() {
                @Override
                public void run() {
                           this.cancel();
                           lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
                           if(lastKnownLocation==null){
                                     locationProvider = LocationManager.GPS_PROVIDER;
                                     // Or use LocationManager.GPS_PROVIDER

                             lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);   
                             return lastKnownLocation;


             }

    },30000);

}
jainal
  • 2,973
  • 2
  • 20
  • 18