Wrong result from location.distanceBetween?

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Wrong result from location.distanceBetween?

Postby w00dy » Tue Mar 30, 2010 6:01 pm

Hallo Guys,

I`m new at the android platform and i hope you can help me.

I write some code to figure out a distance between 2 point.
My 2 points are the following coordinates (i got them from google maps)

Co1: 49.016007, 8.390363
Co2: 49.014519 , 8.394043

and here is my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.app.Activity;
  3.  
  4. import android.content.Context;
  5.  
  6. import android.location.Location;
  7.  
  8. import android.location.LocationListener;
  9.  
  10. import android.location.LocationManager;
  11.  
  12. import android.os.Bundle;
  13.  
  14. import android.util.FloatMath;
  15.  
  16. import android.widget.TextView;
  17.  
  18.  
  19.  
  20. public class LocationViewer extends Activity implements LocationListener {
  21.  
  22.         private LocationManager locationManager;
  23.  
  24.         private TextView textView;
  25.  
  26.        
  27.  
  28.         private static double static_gps_lon = 49.016007;
  29.  
  30.         private static double static_gps_lat = 8.390363;
  31.  
  32.  
  33.  
  34.         @Override
  35.  
  36.         public void onCreate(Bundle savedInstanceState) {
  37.  
  38.                 super.onCreate(savedInstanceState);
  39.  
  40.                 setContentView(R.layout.main);
  41.  
  42.  
  43.  
  44.                 this.textView = (TextView) this.findViewById(R.id.textBox);
  45.  
  46.  
  47.  
  48.                 this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  49.  
  50.  
  51.  
  52.                 for (String provider : this.locationManager.getAllProviders()) {
  53.  
  54.                         this.locationManager
  55.  
  56.                                         .requestLocationUpdates(provider, 1000, 0, this);
  57.  
  58.                 }
  59.  
  60.         }
  61.  
  62.        
  63.  
  64.        
  65.  
  66.         //@SuppressWarnings("static-access")
  67.  
  68.         @SuppressWarnings("static-access")
  69.  
  70.         public synchronized void onLocationChanged(Location location) {
  71.  
  72.                
  73.  
  74.                 float[] distance = new float[2];
  75.  
  76.                 location.distanceBetween(static_gps_lat, static_gps_lon, location.getLatitude(), location.getLongitude(), distance);
  77.  
  78.                
  79.  
  80.                 if (location != null) {
  81.  
  82.                         this.textView.setText("Fix Latitude:\t\t\t" + static_gps_lat
  83.  
  84.                                         + "\nFix Longitude:\t\t\t" + static_gps_lon
  85.  
  86.                                         + "\nLatitude:\t\t\t" + location.getLatitude()
  87.  
  88.                                         + "\nLongitude:\t\t\t" + location.getLongitude()
  89.  
  90.                                         + "\nTime:\t\t\t" + location.getTime() + "\nDistance:\t\t\t" + distance[0]);
  91.  
  92.                 }
  93.  
  94.         }
  95.  
  96.  
  97.  
  98.         public void onProviderDisabled(String provider) {
  99.  
  100.         }
  101.  
  102.  
  103.  
  104.         public void onProviderEnabled(String provider) {
  105.  
  106.         }
  107.  
  108.  
  109.  
  110.         public void onStatusChanged(String provider, int status, Bundle extras) {
  111.  
  112.         }
  113.  
  114. }
Parsed in 0.056 seconds, using GeSHi 1.0.8.4


Co1 are fixed in the code. Co2 send over DDSM (Eclipse) to the Application.

Now the app show me 440meter as result...but thats wrong.

On this site http://www.sunearthtools.com/dp/tools/distance.php i get the right distance between the coordinates..

what is wrong, i have no idea...please help me.

thx a lot.
w00dy
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Mar 30, 2010 5:51 pm

Top

Postby scb8mp » Mon Apr 12, 2010 6:37 pm

How much is it off by? Check what distances are stored in distances[1] and distances [2]
scb8mp
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 09, 2010 9:15 pm

Postby panini » Sat May 01, 2010 1:56 am

try with distanceto function......

Syntax is
location1.distanceto(location2)...result will be in float.
panini
Freshman
Freshman
 
Posts: 2
Joined: Tue Feb 23, 2010 2:44 am

Re: Wrong result from location.distanceBetween?

Postby matrone21 » Mon Jun 20, 2011 9:44 pm

You inverse longitude and latitude in your code.

Google give latitute longitude format.

Put it in correct order and you will get exact result.
matrone21
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jun 20, 2011 9:41 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 3 guests