# Thread: How to calculate distance between to geo points in j2me?

1. ## How to calculate distance between to geo points in j2me?

Hi All,

Below is the code to calculate distance between two geo points.

public double distance_Haversine(double latVal1,double lonVal1,double latVal2,double lonVal2)
{
double dist = 0.0;
double deltaLat = Math.toRadians(latVal2 - latVal1);
double deltaLon = Math.toRadians(lonVal2 - lonVal1);

double a = Math.sin(deltaLat/2) * Math.sin(deltaLat/2) +
Math.cos(latVal1) * Math.cos(latVal2) * Math.sin(deltaLon/2) * Math.sin(deltaLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

return dist;
}

it works fine in android but when i tried to use it in core j2me through net Beans it is giving me error like function atan2 is not supported.

I had imported all the math libraries however my program failed to compile.

Can some one give me any alternative to calculate distance between two Geo points or any alternative math function to replace atan2??

2. ## Re: How to calculate distance between to geo points in j2me?

You can use JSR 179 for this:

Refer to http://discussion.forum.nokia.com/fo...d.php?t=197098

thanks,
~Amitabh

3. ## Re: How to calculate distance between to geo points in j2me?

apart from Math functionalities you can use the following IF you're using the JSR179 API :
- from the JSR179 API you get a Locatio object which contains a Coordinates object
- the Cooridnates object has a method (forgot the name but you can check the JSR179 documentation) which will give you the disctance between the called coordinates object and a second (passed) coordinates object

4. ## Re: How to calculate distance between to geo points in j2me?

the Cooridnates object has a method (forgot the name but you can check the JSR179 documentation) which will give you the disctance between the called coordinates object and a second (passed) coordinates object
distance
public float distance(Coordinates to)Calculates the geodetic distance between the two points according to the ellipsoid model of WGS84. Altitude is neglected from calculations.
The implementation shall calculate this as exactly as it can. However, it is required that the result is within 0.35% of the correct result.

javax.microedition.location.Coordinates
Method name is distance

Parameters:
to - the Coordinates of the destination
Returns:
the distance to the destination in meters
Throws:
java.lang.NullPointerException - if the parameter is null

thanks,
~Amitabh

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•