mardi 25 août 2015

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://ift.tt/1UbmTJY"+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: http://ift.tt/1EgEePF



from Newest questions tagged java - Stack Overflow http://ift.tt/1UbmTK2
via IFTTT

Aucun commentaire:

Enregistrer un commentaire