2

I have been trying to get speed by the help of GPS. I would like to get Gps datas(latitude-longitude-speed) when my telephone fell down.

When telephone fell down, gps datas are obtained and sent to my server. For this aim, I used threads. Each thread get gps datas and send to server them.

My problem is speed value. I got speed calculating distance between two location and time (speed = distance / time)

Thread makes :

@Override
public void run() {

    Looper.prepare();

    float distance = 0.0f;
    float[] results = new float[1];
    Long longValue = new Long(11);
    double firstLatitude;
    double firstLongitude;
    long firstTime;
    float speedOfDevice = 0.0f;

    //for sendin gps datas to web server
    ReportLocation reportObj = new ReportLocation(this);

    locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


    locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

    //latitude, longitude and timeOffix is set in location listener
    firstLatitude = latitude;
    firstLongitude = longitude;
    firstTime = timeOfFix;

    // Waiting 1.5 second 
    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
        e.printStackTrace();
        Toast.makeText(this, "Sleep exception", Toast.LENGTH_LONG).show();
    }


    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);


    if( timeOfFix != firstTime ) {
        Location.distanceBetween(firstLatitude, firstLongitude, latitude, longitude, results);
        distance = results[0];
        longValue = new Long(timeOfFix - firstTime );
        speedOfDevice = 3600 * distance / ( longValue.floatValue() ); 
    }

    try {

        reportObj.send( Double.toString(firstLatitude),  Double.toString(firstLongitude), Float.toString( speedOfDevice ) );

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "Client protokol exception ", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "IO exception "+e.getMessage(), Toast.LENGTH_LONG).show();
    }


    handler.sendEmptyMessage(0);

    Looper.loop();
}

}


class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {

            locManager.removeUpdates(locListener); 

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

        }  
    }

    @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
    }
}

I tried my program in the car that in motion. But I couldn't get correct speed datas.. For example 7.31365 , 8.29663 etc.. They should be 50- 60 or 70 km/h..

What are your thoughts ?

iremce
  • 570
  • 4
  • 14
  • 25
  • Are the locations returned correct? – znat Jan 10 '12 at 18:13
  • Yes, locations returned correct. But firstLatitude and latitude or fistLongitude and longitude values may same sometimes. I don't understand it.. – iremce Jan 10 '12 at 18:16
  • I am wondering if your implementation is correct. The listener should get back to the calling service/thread/activity with a call back (check this post: http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a). In addition, I think it will stop working when the phone goes to sleep mode – znat Jan 10 '12 at 18:40

1 Answers1

0

I am wondering if your implementation is correct. I think the problem is that the location updates are not connected to the fix of a new location. So if your gps did not make a new fix, then the current location will be sent again.

A logic could be that every X seconds, you turn on your requestLocationUpdates() process the location if/when it is received and turn off updates (removeUpdates()) Check this post for an excellent example (that solved many problems for me): What is the simplest and most robust way to get the user's current location on Android?

The example above is clear to understand. The tricky part is to use it in a service as a service does not work when the phone goes on sleep mode. Here you can use a commonsware component to overcome this.

I have used and mixed those two components to set up a location service and it is stable.

Hope it helps

Community
  • 1
  • 1
znat
  • 13,144
  • 17
  • 71
  • 106