This is the formula I am using:
double lat1 = [self getRadiansFromDegress:self.LocationLatitude];
double lat2 = [self getRadiansFromDegress:PointOfInterest.latitude];
double x = sin(lonDiff) * cos(lat2);
double y = cos(lat1)*sin(lat2) - sin(lat1)*cos(lat2)*cos(longDiff);
double bearingRad = atan2(x,y);
double bearingDeg = [self getDegreesFromRadians:bearingRad];
NSLog(#"Bearing: %f", bearingDeg);
For lat1 = 30.61
lat2 = 30.620954
lon1 = -96.342
lon2 = -96.345238
longDiff = -0.003238
According to this website: http://www.yourhomenow.com/house/haversine.html I should be getting about 345 degrees but my code is returning -86.028010 degrees. Can anyone see why this is so different?
in atan2 you exchanged x and y.
The correct order is y,x:
Correct to
double bearingRad = atan2(y, x);
See also:
#include <math.h>
double atan2( double y, double x );
Related
I have user current location i.e. CLLocation Coordinate (location lat & long) and user is on race track pointing to one direction with the help of user current location i created one region now I want some more race track coordinate(say 2m , 4m , 6m away from race track in perpendicular direction) and the track is 10 m long. Please check the image and the red points are on the track.
Please check this image
/**
* Returns the destination point from initial point having travelled the given distance on the
* given initial bearing (bearing normally varies around path followed).
*
* #param {double} distance - Distance travelled, in same units as earth radius (default: metres).
* #param {double} bearing - Initial bearing in degrees from north.
*
* #returns {CLLocationCoordinate} Destination point.
*/
#define kEarthRadius 6378137
- (CLLocationCoordinate2D)destinationPointWithStartingPoint:(MKMapPoint)initialPoint distance:(double)distance andBearing:(double)bearing {
CLLocationCoordinate2D location = MKCoordinateForMapPoint(initialPoint);
double delta = distance / kEarthRadius;
double omega = [self degreesToRadians:bearing];
double phi1 = [self degreesToRadians:location.latitude];
double lambda1 = [self degreesToRadians:location.longitude];
double phi2 = asin(sin(phi1)*cos(delta) + cos(phi1) * sin(delta) * cos(omega));
double x = cos(delta) - sin(phi1) * sin(phi2);
double y = sin(omega) * sin(delta) * cos(phi1);
double lambda2 = lambda1 + atan2(y, x);
return CLLocationCoordinate2DMake([self radiansToDegrees:phi2], ([self radiansToDegrees:lambda2]+540)%360-180);
}
- (CLLocationCoordinate2D)rhumbDestinationPointForInitialPoint:(MKMapPoint)initialPoint distance:(double)distance andBearing:(double)bearing {
CLLocationCoordinate2D location = MKCoordinateForMapPoint(initialPoint);
double delta = distance / kEarthRadius;
double omega = [self degreesToRadians:bearing];
double phi1 = [self degreesToRadians:location.latitude];
double lambda1 = [self degreesToRadians:location.longitude];
double delta_phi = delta * cos(omega);
double phi2 = phi1 + delta_phi;
// check for some daft bugger going past the pole, normalise latitude if so
if (fabs(phi2) > M_PI / 2) {
phi2 = phi2 > 0 ? M_PI-phi2 : -M_PI-phi2;
}
double delta_gamma = log(tan(phi2/2+M_PI/4)/tan(phi1/2+M_PI/4));
double q = fabs(delta_gamma) > 10e-12 ? delta_phi / delta_gamma : cos(phi1);
double delta_lambda = delta*sin(omega)/q;
double lambda2 = lambda1 + delta_lambda;
return CLLocationCoordinate2DMake([self radiansToDegrees:phi2], ([self radiansToDegrees:lambda2]+540)%360-180);
}
- (double)degreesToRadians:(double)degrees {
return degrees * M_PI / 180.0;
}
- (double)radiansToDegrees:(double)radians {
return radians * 180.0 / M_PI;
}
Adapted from : http://www.movable-type.co.uk/scripts/latlong.html
More information on bearing : https://en.wikipedia.org/wiki/Bearing_(navigation)
And rhumb line : https://en.wikipedia.org/wiki/Rhumb_line
I am trying to get panoramio picture around a given coordinate. However always my query returns zero photos. This is the code I am using.
const double WGS84_a = 6378137.0;
const double WGS84_b = 6356752.3;
double Deg2rad(double degrees) {
return degrees * M_PI / 180.0;
}
double Rad2deg(double radians) {
return radians * 180.0 / M_PI;
}
double WGS84EarthRadius(double lat)
{
double An = WGS84_a*WGS84_a * cos(lat);
double Bn = WGS84_b*WGS84_b * sin(lat);
double Ad = WGS84_a * cos(lat);
double Bd = WGS84_b * sin(lat);
return sqrt( (An*An + Bn*Bn)/(Ad*Ad + Bd*Bd) );
}
MapRect LatLonDestPoint(CLLocationCoordinate2D origin, double halfSideInKm) {
double lat = Deg2rad(origin.latitude);
double lon = Deg2rad(origin.longitude);
double halfSide = 1000*halfSideInKm;
double radius = WGS84EarthRadius(lat);
double pradius = radius*cos(lat);
double latMin = lat - halfSide/radius;
double latMax = lat + halfSide/radius;
double lonMin = lon - halfSide/pradius;
double lonMax = lon + halfSide/pradius;
return MKMapRectMake(Rad2deg(latMin), Rad2deg(lonMin), Rad2deg(latMax), Rad2deg(lonMax));
}
Now for a coordinate (60.1190935704,-149.439081366) I get the API like
http://www.panoramio.com/map/get_panoramas.php?set=public&from=0&to=20&minx=60.029034&miny=-149.619843&maxx=60.209152&maxy=-149.258316&size=medium&mapfilter=true
This always returns me zero results. Please help me with what I am doing wrong.
You have x and y coordinates the wrong way around.
Swapping these returns:-
{"count":379,"has_more":true,"map_location":{"lat":60.118290103595498,"lon":-149.45469385385852,"panoramio_zoom":6}
etc
ps Do not rely on the count being correct. Use the has_more flag.
I am working on an app which has a functionality of RADAR just like LOVOO app. I don't have experience of working on CoreLocation and other location based frameworks.
It would be much appreciated if you could suggest me how should this can be achieved.
What frameworks should i use and how to proceed initially.
Though same question already exists on SO over here my question is same as Radar View like LOVOO but its of no use to me thats why i am asking it again.
What i have tried myself so far is, i have lat and long values of points to plot and i have calculated angle and distance between centre point(my location) and other point
- (float)angletoCoordinate:(CLLocationCoordinate2D)second {
//myCurrentLocation is origin
//second is point
float lat1 = DegreesToRadians(myCurrentLocation.coordinate.latitude);
float lon1 = DegreesToRadians(myCurrentLocation.coordinate.longitude);
float lat2 = DegreesToRadians(second.latitude);
float lon2 = DegreesToRadians(second.longitude);
float dLon = lon2 - lon1;
float y = sin(dLon) * cos(lat2);
float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
float radiansBearing = atan2(y, x);
if(radiansBearing < 0.0)
{
radiansBearing += 2*M_PI;
}
return radiansBearing;
}
-(float)calculateXPointWithLoc:(ARGeoLocation *)loc andDelta:(float)delta{
float angle = radiansToDegrees(delta);
float dpx = (([myCurrentLocation distanceFromLocation:loc.geoLocation])/1000);
if(0<=angle<=90)
return viewRadar.center.x + sin(angle)*dpx ;
else if(90<angle<=180)
return viewRadar.center.x + cos(angle-90)*dpx ;
else if(180<angle<=270)
return viewRadar.center.x - cos(270-angle)*dpx ;
else if(270<angle<360)
return viewRadar.center.x - sin(360-angle)*dpx ;
return 0;
}
-(float)calculateYPointWithLoc:(ARGeoLocation *)loc andDelta:(float)delta{
float angle = radiansToDegrees(delta);
float dpx = (([myCurrentLocation distanceFromLocation:loc.geoLocation])/1000);
if(0<=angle<=90)
return viewRadar.center.y - cos(angle)*dpx ;
else if(90<angle<=180)
return viewRadar.center.y + sin(angle-90)*dpx ;
else if(180<angle<=270)
return viewRadar.center.y + sin(270-angle)*dpx ;
else if(270<angle<360)
return viewRadar.center.y - cos(360-angle)*dpx ;
return 0;
}
and then
int i = 0;
for(ARGeoLocation *loc in coordinates){
deltaAz = [self angletoCoordinate:loc.geoLocation.coordinate];
x = [self calculateXPointWithLoc:loc andDelta:deltaAz];
y = [self calculateYPointWithLoc:loc andDelta:deltaAz];
[[plots objectAtIndex:i] setFrame:CGRectMake(x, y, DIAMETER_PLOT, DIAMETER_PLOT)];
i++;
}
I am not sure whether x and y are correct or not also if they are correct then how can i change these value with change of slider value.
I think the keyword here is Geofencing.
Geofencing is the automatic triggering of an action if your device enters or leaves a certain region. For your case your action is to display the profiles of those users who enter the area of your radar.
Basically you need to calculate a circular region (given a radius) and display all other points within your region.
I once found this tutorial which could teach how to do it by yourself:
http://www.raywenderlich.com/95014/geofencing-ios-swift
I hope it helps!
I need to find the angle between the north and a point in a map (21.4167, -39.8167).
here is my code
double MECCA_LONGITUDE = 21.4167;
double MECCA_LATITUDE = -39.8167;
-(void) getAngle :(float) phoneAngle: (float) lon:(float) lat
{
float ad = atan (sin (lon - MECCA_LATITUDE) / ( (cos (lat)) * (tan (MECCA_LATITUDE)) ) - ( (sin (lat)) * (cos (lon - MECCA_LONGITUDE)) ));
NSLog(#"###### %f", CC_RADIANS_TO_DEGREES(ad));
}
I'm still not getting the correct answer. Any tips would be appreciated. Thanks.
Your principle problem is that sin() and cos() take their arguments in radians, and you are passing degrees (you also have the longitude and latitude of Mecca interchanged).
Try:
double MECCA_LATITUDE = 21.4167;
double MECCA_LONGITUDE = 39.8167;
double r_delta_lon = CC_DEGREES_TO_RADIANS(MECCA_LONGITUDE - lon);
double r_lat2 = CC_DEGREES_TO_RADIANS(MECCA_LATITUDE);
double r_lon1 = CC_DEGREES_TO_RADIANS(lon);
double r_lat1 = CC_DEGREES_TO_RADIANS(lat);
double ad = atan2(sin(r_delta_lon) * cos(r_lat2),
cos(r_lat1) * sin(r_lat2) - sin(r_lat1) * cos(r_lat2) * cos(r_delta_lon));
Very "simple" problem: given two CLLocationCoordinate2Ds, how can I get the bearing (as radians) from the first to the second? I've done a lot of research and studying on this, both the general problem and Objective-C/Cocoa Touch/iOS specifically.
Here's my implementation:
- (float) getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
float fLat = fromLoc.latitude;
float fLng = fromLoc.longitude;
float tLat = toLoc.latitude;
float tLng = toLoc.longitude;
return atan2(sin(fLng-tLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(fLng-tLng));
}
However, this method isn't returning consistant results for me. If the bearing is close to due north or due south, it seems to be fine, however, any other direction seems to return inconsistant data, for example:
From 50.405018, 8.437500
To 51.339802, 12.403340
My method returns: 5.918441 radians
Should be 1.18660576 radians
(see http://www.movable-type.co.uk/scripts/latlong.html and http://www.movable-type.co.uk/scripts/latlong-map.html?lat1=50.405018&long1=8.437500&lat2=51.339802&long2=12.403340)
I've double and triple checked the formula is correct. I've also spot checked a bunch of values like the example above, some correct, some wrong. I've played around with various modulos or bounding of the return value, also no luck.
Any ideas? Is there an issue with my code? Maybe I've misunderstood something about how math functions work?
Here the code modified with the changes suggested by Oren Trutner and from myself:
#define degreesToRadians(x) (M_PI * x / 180.0)
#define radiansToDegrees(x) (x * 180.0 / M_PI)
- (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
float fLat = degreesToRadians(fromLoc.latitude);
float fLng = degreesToRadians(fromLoc.longitude);
float tLat = degreesToRadians(toLoc.latitude);
float tLng = degreesToRadians(toLoc.longitude);
float degree = radiansToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));
if (degree >= 0) {
return degree;
} else {
return 360+degree;
}
}
Your math is correct, with the following exceptions:
Make sure to convert fLat, fLon, tLat, and tLon to radians before applying any sin() or cos() to them. Divide by 180.0 and multiply by PI.
Enter the delta between tLng and fLng as tLng-fLng, and not the other way around. Note that this difference appears twice in the expression.
With those changes, I am getting 1.18660677830947 radians with double precision math and the values in the question.
Swift 3:
extension CLLocationCoordinate2D {
func bearing(to point: CLLocationCoordinate2D) -> Double {
func degreesToRadians(_ degrees: Double) -> Double { return degrees * Double.pi / 180.0 }
func radiansToDegrees(_ radians: Double) -> Double { return radians * 180.0 / Double.pi }
let lat1 = degreesToRadians(latitude)
let lon1 = degreesToRadians(longitude)
let lat2 = degreesToRadians(point.latitude);
let lon2 = degreesToRadians(point.longitude);
let dLon = lon2 - lon1;
let y = sin(dLon) * cos(lat2);
let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
let radiansBearing = atan2(y, x);
return radiansToDegrees(radiansBearing)
}
}
you can use my code.. it's work on my project with microcontroller that use GPS for data.
#define d2r ((22/7.0)/180.0)
#define r2d (180.0/(22/7.0))
double get_heading1(double lat1, double long1, double lat2, double long2)
{
double diff_lat, diff_long;
double degree;
diff_long =(double) (((long2*1000000)-(long1*1000000))/1000000) * d2r;
diff_lat = (double) (((lat2*1000000)-(lat1*1000000))/1000000) * d2r;
degree = r2d (atan2(sin(diff_long)*cos(d2r*lat2),cos(d2r*lat1)*sin(d2r*lat2)-sin(d2r*lat1)*cos(d2r*lat2) *cos(diff_long)));
if (degree >= 0) {
return degree;
} else {
return 360+degree;
}
}