-1

I want user location. both network and GPS location. i am getting network location but GPS location is always null. here is the code that i write for that.

public class GetLocation extends Service implements LocationListener {

    LocationManager locationManager;
    Location networkLocation, gpsLocation;
    Intent intentBroadcastLocationUpdate;
    boolean gpslocationOn, networklocationOn;
    Handler handler = new Handler();

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            broadcastLocation();

            handler.postDelayed(this, 3000);
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        intentBroadcastLocationUpdate = new Intent("UPDATE_LOCATION");

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1000 * 60 * 1, this);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 1000 * 60 * 1, this);

        networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        networklocationOn = true;
        gpslocationOn = true;

        broadcastLocation();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handler.postDelayed(runnable, 3000);
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {
        Toast.makeText(getApplicationContext(),
            location.getProvider() + " Location Changed:" + location.getLatitude() + " , "
                + location.getLongitude(), Toast.LENGTH_LONG).show();
    };

    @Override
    public void onProviderDisabled(String arg0) {
        Toast.makeText(getApplicationContext(), arg0 + " Disabled", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        if (provider.equals("network")) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 1000 * 60 * 1, this);
        }
        if (provider.equals("gps")) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1000 * 60 * 1, this);
        }
        Toast.makeText(getApplicationContext(), provider + " Enabled", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        locationManager.removeUpdates(this);
        handler.removeCallbacks(runnable);    
    }

    private void broadcastLocation() {
        if (gpsLocation == null && networkLocation == null) {
            Toast.makeText(getApplicationContext(),
                    "on create Couldn't get user location", Toast.LENGTH_LONG).show();
            intentBroadcastLocationUpdate.putExtra("loc", "Could not retrive location");
            sendBroadcast(intentBroadcastLocationUpdate);
        } else if (gpsLocation != null) {
            intentBroadcastLocationUpdate.putExtra("loc", "GPS:" + gpsLocation.getLatitude() + ","
                    + gpsLocation.getLongitude());
            sendBroadcast(intentBroadcastLocationUpdate);
        } else if (networkLocation != null) {
            intentBroadcastLocationUpdate.putExtra("loc", "Net:" + networkLocation.getLatitude() + ","
                    + networkLocation.getLongitude());
            sendBroadcast(intentBroadcastLocationUpdate);
        } else if (gpsLocation != null && networkLocation != null) {
            intentBroadcastLocationUpdate.putExtra(
                    "loc",
                    "Net:" + networkLocation.getLatitude() + ","
                            + networkLocation.getLongitude() + " AND GPS:"+ gpsLocation.getLatitude() + ","
                            + gpsLocation.getLongitude());
            sendBroadcast(intentBroadcastLocationUpdate);
        }
    }

}

Can anybody tell me what is wrong that i am doing? why i'm unable to get GPS location? and what is the best practice of getting GPS location?

SilentKiller
  • 6,944
  • 6
  • 40
  • 75
Usman Riaz
  • 2,920
  • 10
  • 43
  • 66

2 Answers2

0

Add these permissions in your manifest

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

Refer this tutorial

onkar
  • 4,427
  • 10
  • 52
  • 89
0
public void getMyLocation() {
LocationManager locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = locationManager.getProviders(true);

    Location l = null;
    for (int i = 0; i < providers.size(); i++) {
        l = locationManager.getLastKnownLocation(providers.get(i));
        if (l != null)
            break;
    }
    if (l != null) {
        latitude = l.getLatitude();
        longitude = l.getLongitude();
    }
}
John
  • 8,846
  • 8
  • 50
  • 85