Skip to content Skip to sidebar Skip to footer

Getting Current Location From Gps

Using Criteria.ACCURACY_COARSE I get the location but it is not accurate. I wanted to use GPS Criteria.ACCURACY_FINE but the onLocationChanged(Location location) method is not call

Solution 1:

Here you go

package com.test.locationmanager;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

publicclassLocationManagerStatusextendsActivity {

privateLocationManager locationManager;
privateTextView textView;
private final LocationListener gpsLocationListener = newLocationListener() {

    @OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {
        final String tvTxt = textView.getText().toString();
        switch (status) {
        caseLocationProvider.AVAILABLE:
            textView.setText(tvTxt + "GPS available again\n");
            break;
        caseLocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "GPS out of service\n");
            break;
        caseLocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt + "GPS temporarily unavailable\n");
            break;
        }
    }

    @OverridepublicvoidonProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Enabled\n");
    }

    @OverridepublicvoidonProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "GPS Provider Disabled\n");
    }

    @OverridepublicvoidonLocationChanged(Location location) {
        locationManager.removeUpdates(networkLocationListener);
        textView.setText(textView.getText().toString()
                + "New GPS location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};
private final LocationListener networkLocationListener = newLocationListener() {

    @OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {
        final String tvTxt = textView.getText().toString();
        switch (status) {
        caseLocationProvider.AVAILABLE:
            textView.setText(tvTxt + "Network location available again\n");
            break;
        caseLocationProvider.OUT_OF_SERVICE:
            textView.setText(tvTxt + "Network location out of service\n");
            break;
        caseLocationProvider.TEMPORARILY_UNAVAILABLE:
            textView.setText(tvTxt
                    + "Network location temporarily unavailable\n");
            break;
        }
    }

    @OverridepublicvoidonProviderEnabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Enabled\n");
    }

    @OverridepublicvoidonProviderDisabled(String provider) {
        textView.setText(textView.getText().toString()
                + "Network Provider Disabled\n");
    }

    @OverridepublicvoidonLocationChanged(Location location) {
        textView.setText(textView.getText().toString()
                + "New network location: "
                + String.format("%9.6f", location.getLatitude()) + ", "
                + String.format("%9.6f", location.getLongitude()) + "\n");
    }
};

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.textview);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}

@OverrideprotectedvoidonResume() {
    super.onResume();
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 5000, 0,
            networkLocationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            3000, 0, gpsLocationListener);
}

@OverrideprotectedvoidonPause() {
    super.onPause();
    locationManager.removeUpdates(networkLocationListener);
    locationManager.removeUpdates(gpsLocationListener);
}
}

Solution 2:

The new Google Maps API introduces OnMyLocationChangeListener. You don't need to implement LocationListener. Check this answer: https://stackoverflow.com/a/16240460/1769013

Solution 3:

Well after nearly 2 months without a response, I decide to go with the following which works. Hope it will help other people saving some time.

publicclassGpsLocationServiceextendsServiceimplementsLocationListener{

    private final StringTAG = getClass().getSimpleName();

    privateLocationManager locationManager;
    privateString provider;

    @OverridepublicvoidonCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate()");
        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use// defaultCriteria criteria = newCriteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fieldsif (location != null) {
            Log.d(TAG, "Provider " + provider + " has been selected.");
            onLocationChanged(location);
        } 
        else {
            Log.d(TAG, "Location not available!");
        }

    }

    @Overridepublic int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "GpsLocationService has started!");


        locationManager.requestLocationUpdates(provider, 400, 1, this);

        returnSTART_STICKY;
    }

    @OverridepublicvoidonDestroy() {
        Log.i(TAG, "GpsLocationService is destroyed!");
        super.onDestroy();
        locationManager.removeUpdates(this);
    }

    @Overridepublic IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stubreturnnull;
    }



    @OverridepublicvoidonLocationChanged(Location location) {
        double lat = location.getLatitude();
        double lng =  location.getLongitude();
        Log.i( TAG, "onLocationChanged() lat = " + lat + " long = " + lng);     
    }

    @OverridepublicvoidonProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @OverridepublicvoidonProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

And in your manifest file don't forget to add the following permissions:

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

Mention your service also:

<serviceandroid:name=".GpsLocationService"></service>

Post a Comment for "Getting Current Location From Gps"