0

I want to receive a float value from DDMS but it sends an integer Example:

longitude:2.351696 latitude:48.857593

But i receive:

longitude:2.0 latitude:48.0

How can I make this work?

Here is my code:

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    }


    @Override
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        if (location != null) {
            Toast.makeText(this, "ma position :" + latitude + ", " + longitude,
                    Toast.LENGTH_LONG).show();
            Log.i("latitude", "" + latitude);
            Log.i("longitude", "" + longitude);

        }

the Log:

03-27 09:25:20.163: I/latitude(793): 48.0 03-27 09:25:20.163: I/longitude(793): 2.0


private double getDistanceBetweenPoints(double lat1, double long2, double arrive_lat,double arrive_long) {

        int R = 6371; // km

        double dLat = Math.toRadians(arrive_lat - lat1);
        double dLon = Math.toRadians(arrive_long - long2);

        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(arrive_lat)) * Math.sin(dLon / 2)
                * Math.sin(dLon / 2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double d = R * c;

        return d*1000;
    }
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
Wassim
  • 13
  • 5
  • DDMS is the ADK Debug Monitor Service. Are you just seeing Integer's in the Logcat output? If so please post the code that is outputting the messages – pmckeown Mar 27 '12 at 10:52
  • I use these values to calculate the distance between 2 points gps and i have a wrong result so the values from DDMS are wrong. – Wassim Mar 27 '12 at 11:01
  • `Location.getLatitude()` returns a double, and a double is being output in logcat (48.0). I would debug your app and check the values of latitude and longitude at runtime. – pmckeown Mar 27 '12 at 11:02
  • Also, are you running it in the emulator or a Phone with GPS? – pmckeown Mar 27 '12 at 11:03
  • Log.i("distance1", ""+this.getDistanceBetweenPoints(48.857593,2.351696,48.858786, 2.348435)); Log.i("distance2", ""+this.getDistanceBetweenPoints(latitude,longitude,48.858786, 2.348435)); the firt distance is true when i put a static latiude and longitude but the second distance is wrong when i use the value from DDMS – Wassim Mar 27 '12 at 11:07
  • what is the type you assigned to latitude and longitude? – Rekha Mar 27 '12 at 11:15
  • double latitude double longitude – Wassim Mar 27 '12 at 11:18

1 Answers1

1

Running in the emulator is the problem here. Your PC won't have a a GPS chip, so you won't get very accurate Coordinates.

For more information and help on how to mock accurate geo locations: http://developer.android.com/guide/topics/location/obtaining-user-location.html

And previously discussed on stackoverflow.... GPS accuracy of Android Emulator

Community
  • 1
  • 1
pmckeown
  • 3,419
  • 3
  • 31
  • 35
  • Log.i("distance1", ""+this.getDistanceBetweenPoints(48.857593,2.351696,48.864694, 2.351922)); Log.i("distance2", ""+this.getDistanceBetweenPoints(latitude,longitude,48.864694, 2.351922)); the firs distance is true when i put static value but the second is wrong when i use value from DDMS – Wassim Mar 27 '12 at 11:15
  • You'll need to post the code in your getDistanceBetweenPoints() method before I could possibly comment on it ;) – pmckeown Mar 27 '12 at 11:18
  • i post it but there is no wrong in the code of the methode; the question is how can i receive float value from ddms not an integer :) – Wassim Mar 27 '12 at 11:21
  • I would advise that you write some Unit Tests to assert that the output of your distanceBetweenTwoPoints method and discover if it is actually working correctly. – pmckeown Mar 27 '12 at 11:33
  • it worked because i return a right result in the Log "distance1" – Wassim Mar 27 '12 at 11:34