0

I'm trying to use Lat & Lon values for a function I created, but I can't find a way how to get this values from the class "Locationer" to the MainActivity. I have created a new method called getLat(), but I need a location variable in order to get the latitude value with this method. I have no idea how to get this "location" and where it comes from in the class. I've am testing it on my own device.

The Locationer class:

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;

public class Locationer extends Activity implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        location.getLatitude();
        location.getLongitude();

        String myLocation = "Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude();

        //I make a log to see the results
        Log.e("MY CURRENT LOCATION", myLocation);

    }

    public double getLat(Location location)
    {
        return location.getLatitude();
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
}

the code in my MainAcitivity.java:

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        Locationer locationListener = new Locationer();

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        lat = **NEED YOUR HELP TO GET THIS VALUE**
        lon = **NEED YOUR HELP TO GET THIS VALUE**

        queryBooks(lat, lon);
tyczj
  • 71,600
  • 54
  • 194
  • 296
TheUnreal
  • 23,434
  • 46
  • 157
  • 277

1 Answers1

0

You will have no latitude or longitude directly after setting the LocationManager. The onLocationChanged() will be called when it find a location. See here.

You can do the queryBooks(lat, lon); in the onLocationChanged().

Or you can use getLastKnownLocation(LocationManager.GPS_PROVIDER); but it will be less accurate and can provide you an old location.

Here what you can do in you MainActivity :

private LocationManager mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

private LocationListener mLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        queryBooks(location.getLatitude(), location.getLongitude());
    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }


};

protected void onResume() {
    super.onResume();
    //every second
    int minTime = 1000;
    //minDistance between two update
    int minDistance = 10;
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, mLocationListener);
};

@Override
protected void onPause() {
    mLocationManager.removeUpdates(mLocationListener);
    super.onPause();
}
  • The question is if it's the best solution? I will have to move alot of my code to the location class and what happens if I will have multiple pages using the GPS system? – TheUnreal Aug 21 '15 at 17:13
  • it's better to get the location in the onLocationChanged. To avoid move all your code in the Locationer class, just create the interface in your MainActivity – Aurélien Guelle Aug 22 '15 at 12:25
  • What do you mean by creating interface in the MainActivity? I already have one. – TheUnreal Aug 22 '15 at 15:54