Pages

Sunday, November 28, 2010

Calculate New Lat/Long from distance and lat/long

float DegreesToRadians = M_PI/180;
float RadiansToDegrees = 180 / M_PI;
float EarthRadius = 6378137.0;
float oldlat=23.047744;
float oldlong=72.566013;
float range = 1000; // range in Meter 1000 = 1 km
float bearing = 0;
double latA = oldlat * DegreesToRadians;
    double lonA = oldlong * DegreesToRadians;
    double angularDistance = range / EarthRadius;
    double trueCourse = bearing * DegreesToRadians;
    double lat = asin(
  sin(latA) * cos(angularDistance) + 
  cos(latA) * sin(angularDistance) * cos(trueCourse));
    double dlon = atan2(
sin(trueCourse) * sin(angularDistance) * cos(latA), 
cos(angularDistance) - sin(latA) * sin(lat));
//B % A = B - (floor(B / A) * A)
float B = (lonA + dlon + M_PI);
float A = 2*M_PI;
    double lon =  B - (floor(B / A) * A)- M_PI;
lat = lat * RadiansToDegrees;
lon = lon * RadiansToDegrees; 
//find distance by google map by using both lat/long.
NSLog(@"lat = %f...long  = %f...M_PI = %f",lat,lon,M_PI);

No comments:

Post a Comment