Skip to content Skip to sidebar Skip to footer

Get User Current And Exact Location Using Gps

For my new app, I need to get user location using GPS. To do so, I am using fusedLocationProviderApi.getLastLocation. Most of the time I am getting null. But then I realized that a

Solution 1:

You can use this code in OnCreate() method to get location

// 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.
      makeUseOfNewLocation(location);
    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

Post a Comment for "Get User Current And Exact Location Using Gps"