简体   繁体   中英

GPS Coords being always 0.0-0.0 using LocationListener in a Android App

I need to develop an app for android for a university task and a part of it consists in sending the GPS coords of the device to my database. Here's the class that I'm using to get the coords

public class MyLocationListener implements LocationListener {

double latitude;
double  longitude;


public MyLocationListener(Context myContext){
    LocationManager locationManager = (LocationManager) myContext.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

public  double getLatitude(){
    return latitude; }

public  double getLongitude(){
    return longitude; }

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

   latitude =  location.getLatitude();
    longitude =  location.getLongitude();

}

@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

}

}

That is called inside InterfaceActivity.java, using this:

        MyLocationListener ggwp = new MyLocationListener(InterfaceActivity.this);
    HttpDevices elenco = new HttpDevices(LoginIstance.getIst().getLog()[0],LoginIstance.getIst().getLog()[1],LoginIstance.getIst().getID(),String.valueOf(ggwp.getLatitude()),String.valueOf(ggwp.getLongitude()));

And finally HttpDevices is an asynttask that "runs" http messages to my database, using this code:

            HttpPost httppost = new HttpPost("http://88.116.86.82/android/remote/connessione.php?user="+usr+"&pass="+pss+"&id="+id+"&lat="+lat+"&long="+log);

But the coords send to the database are always 0.0 - 0.0 , and I can't even try the code an debug it on the emulator ( blue stack ) because it doesn't have gps coords. Tried it on my Moto G but they are just 0. I tried spamming that string (connessione.php etc.. ) in my broweser giving random values for lat and long and yes, it works. It updates the database and shows the coords in my app, so the problem should be in the java. Any tips? I've searched alot in google/stackoverflow and didnt' find anything, it's the first time I encouter a problem that requires me to post here. Hope you can help me! Thank youu!

Edit: in case you want to take a look at my whole code there is a github link: https://github.com/leejaku/RemoteAndroid2

I just try to get through your code... Please correct me if I have missed something

If I look at your HttpDevices Class you try to send your Data like this...

lat = String.valueOf(MyLocationListener.latitude);
log = String.valueOf(MyLocationListener.longitude);

This will try to get the value of a class property which is 0.0, because it is not the instance of MyLocationListener that is updating the lat and long... you need to create a Singleton of MyLocationListener to get one single sharedInstance of your listener, or you have to do it with a Delegate Pattern, or you save the Values to SharedPreferences...

Do you have enough knowledge of Java to build one of these Paradigms, and does it make sense to you? (Sorry for that Question, I don't know how good you are mate :-)...) Otherwise I could write you something to solve it

UPDATE

Well this problem seems to be nasty, so we will need a "NastyLocationListener" to solve it, and thank Neo has written the ultimate one ;-)

Checkout this Class https://gist.github.com/DennisWeidmann/de2ebb2b2549a938fa50

Usage just like this

/////////Just to know it
        Log.e("lati", NastyLocationListener.getLatitude(this)); //On most phones you can use the Listener without any other Code
        Log.e("longi", NastyLocationListener.getLongitude(this)); //It uses Androids native cached location but this is not updated proper
        /////////Just to know it end

        /////////Normal usage
        nastyLocationListener = new NastyLocationListener(this); //In your MainActivity instanciate a NastyLocationListener (its only needed once... All other Activities could get data from him)
        nastyLocationListener.startListening(); //It starts listening to Location Updates and triggers the Location Provider to fetch updates (its bool, true if almost one Provider is available, false if no Provider is available)

        nastyLocationListener.stopListening(); //Use this line when you pause the app, or quit it... it will stop the listener and could be resumed with startListening()

        NastyLocationListener.getLatitude(this); //Could be used in every class, on every time, however you want without any other code like above
        NastyLocationListener.getLongitude(this); //Could be used in every class, on every time, however you want without any other code like above
        /////////Normal usage end

UPDATE

Screenshot of your App on my device without changing any code instead of the REST Call, I have inserted a Log to output my Latitude..

在此处输入图片说明

Try with this class, it's from my github account: https://github.com/chiiiky14/GPStracker

Import it to your project and create the object:

GPSTracker gpstracker = new GPSTracker(context);

And after that:

  gpstracker.getLatitude();
  gpstracker.getLongitude();

Just make sure the GPS of your device is already activate. Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM