distance between two GeoPoints in SDK1.0

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

distance between two GeoPoints in SDK1.0

Postby zak » Tue Dec 30, 2008 6:42 pm

hi!! i need an help!!!
how to get distance between two GeoPoints in sdk 1.0 ? MapPoint.distanceSquared(MapPoint) is gone :(

Posts: 8
Joined: Tue Sep 23, 2008 8:42 am


Postby dmt » Thu Jan 08, 2009 1:32 am

Are you talking about computing the distance between 2 points on the surface of a sphere?

If so, you'll need to brush up on your trigonometry, and first compute the Haversine function (this is the standard way of doing it). In order to use the Java trig functions, you'll have to first convert all your angles from degrees to radians.
Given two longitude/latitude pairs, and the earth's average radius (assume 6356.78km for your calculations),
you can calculate the distance between the 2 points via this Java code:

double EarthRad = 6356.78; // in km !
// first convert to radians...
double geo1_lat = geo1.getLatitude()*java.lang.Math.PI/360;
double geo1_lng = geo1.getLongitude()*java.lang.Math.PI/360;
double geo2_lat = geo2.getLatitude()*java.lang.Math.PI/360;
double geo2_lng = geo2.getLongitude()*java.lang.Math.PI/360;

double deltaLat = java.lang.Math.abs(java.lang.Math.abs(geo2_lat) - java.lang.Math.abs(geo1_lat));
double deltaLng = java.lang.Math.abs(java.lang.Math.abs(geo2_lng) - java.lang.Math.abs(geo1_lng));

double dist = 2*EarthRad*java.lang.Math.asin(java.lang.Math.sqrt(haversine(deltaLat)
+ java.lang.Math.cos(pair1_lat)

Where "dist" now contains the distance between along the earth's surface.
You can find the Haversine function trig equation by Googling it, then construct a method that returns the appropriate value.
User avatar
Junior Developer
Junior Developer
Posts: 15
Joined: Mon Jan 05, 2009 6:06 am

Postby dmt » Thu Jan 08, 2009 1:34 am

Addendum: pls. replace "pair1_lat" with "geo1_lat"
and "pair2_lng" with "geo2_lng"
User avatar
Junior Developer
Junior Developer
Posts: 15
Joined: Mon Jan 05, 2009 6:06 am

It`s now in the Location class...

Postby jbrohan » Mon Feb 01, 2010 3:28 am

There is a public member of the Location class called

ublic static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

Since: API Level 1
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.
The computed distance is stored in results[0]. If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2].
startLatitude the starting latitude
startLongitude the starting longitude
endLatitude the ending latitude
endLongitude the ending longitude
results an array of floats to hold the results
IllegalArgumentException if results is null or has length < 1

merci beaucoup Google
Junior Developer
Junior Developer
Posts: 14
Joined: Fri Aug 28, 2009 11:12 am
Location: Montreal


Return to Map Problems

Who is online

Users browsing this forum: No registered users and 2 guests