Calculating the Bearing of A(x1,y1) Relative to B(x2,y2) in iOS - iphone

May I know how I should calculate the bearing of one point relative to another? All the formulas I'm seeing on the Internet are for lat/lon coordinates. I'm working with the Cartesian coordinate system here and am unable to find a solution. Please help!

Use atan2 function (IIRC your objective-c should have it).
It gives you a result between -PI and PI. You have to map it to 0-360 if you need it.

Not really an answer to your question, but if you're getting your points using CLLocationManager, each CLLocation has a course property, which gives you the bearing of your journey for the given point. For the actual math, see Axeman's answer.

Related

iOS is it possible to convert CLLocation into some sort of XYZ metric coordinate system?

I'm building an augmented reality game, and working with CLLocation is rather cumbersome.
Is there some way to locally approximate CLLocation as XYZ coordinate, expressed in meters with the origin starting at some arbitrary point (for example the initial position when the game was started)?
Lets say I'm working with a 1 mile radius and do not really care about the curvature of the earth. Is it possible to approximate or somehow simplify the location based calculations for local position tracking?
Alternatively, is there a coordinate system that can be used with CLLocation that also incorporates the roll, pitch, yaw of the CMAttitude as well as compass orientation?
Clarification: As far as I understand, the problem with latitude and longitude is that their units vary in size, depending on the position on the globe. I should've specified that X,Y,Z should be in standard units, like meters or feet.
Thank you!
The Haversine formula may be useful.
I found a good article on it at http://www.jaimerios.com/?p=39 with code examples.
You could get the initial point at the app's launch and calculate the relative points based on the user coordinates as he or she moves. Admittedly, this is not super elegant, but if you are just trying to do some simple comparisons based on the user's location relative to an arbitrary origin, this should work. For the Z, Alex Stone's suggestion of calculating it based on the altitude should be fine.

IOS Core location - Find out nearest branch

I have create a program to find out my current location using IOS corelocation framework.It works fine.
I need to create a program that list nearest branchaes of a shop chain, while a user travel with the phone.My data base contains the branches details with Latitude and lognitude.How i compare with these details to find out the nearest branch.
Help is highly appreciated.Anybody knows any example program
Thanks,
VKS
You can make use of distanceFromLocation: method of CLLocation.
distanceFromLocation:
Returns the distance (in meters) from the receiver’s location to the
specified location.
(CLLocationDistance)distanceFromLocation:(const CLLocation
*)location
Parameters
location
The other location.
Return Value
The distance (in meters) between the two locations.
Discussion
This method measures the distance between the two locations by tracing
a line between them that follows the curvature of the Earth. The
resulting arc is a smooth curve and does not take into account
specific altitude changes between the two locations.
Availability
Available in iOS 3.2 and later.
By using this method to get the distances of the shops with your current location, you can then sort the list based on the distance to get the nearest branch
VKS, I got to solve the exact same problem too now (using apptarget iOS 5). Do you have any example code / snippets / tips to share for this problem, that I assumed you solved a long time ago? The above is very good explanation from 7KV7, but some examples will speed up my process a lot. Thank's :-)

How can we find the angle between two locations defined by latitude or longitude

I do not want any code but want to get reference that how can we find the angle between two locations defined by Latitude or Longitude...
If you do have reference then Please help me to solve my problem...
Thanx in advance....
The formula to calculate bearing is:
θ = atan2( sin(Δlong).cos(lat2), cos(lat1).sin(lat2) − sin(lat1).cos(lat2).cos(Δlong) )
Bearing is a direction to move from one location to another location (starting from north and then clockwise). While angle in 2D starts from the east and then counter clockwise. So if an angle is what you need, later you'll need to add 90 degree to the result and then revert it (add minus).
Reference:
http://www.movable-type.co.uk/scripts/latlong.html
try the Atan method
Math.Atan2(x1-x2,y1-y2)
Try this website, it does all the calculations for you. Whereas the formulas, those can be found on wiki or any other sites. I like this site cos it managed to help me settle a lot of problems. And even if this web tool was developed in San Francisco, even I am from Singapore. This will work.
Latitude and Longitude web tool

How To calculate nearest location from user?

In my application i am having 4 places with its longitude and altitude(Given).Now i want to find the nearest place from user.
So anyone can tell me the solution for it or provide me some source code or demo for this.
Thanks to all
Create an CLLocation of the points:
CLLocation* locationx = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
And the compare the points with:
double distance = [userposition distanceFromLocation:locationx];
Keep the nearest location.
Edit:
If you really mean "longitude and altitude" then I dont know how.
just took 10 seconds to ask google and find this, wich describes how to calculate the distance beween two points given latitude and logitude:
http://www.movable-type.co.uk/scripts/latlong.html
all you have to do is calculate the distance to all 4 points and choose the lowest one.
ETDI: i'm sure you're also given the latitude, otherwise you won't even have specific coordinates. if this wasn't a type and you really have to take the altitude into account, take a look at this question at google answers
picknick describes a native class method.
This is probably going to use the Haversine formula (search the web or stackoverflow for discussions) to measure distances.
In my experience, writing your own Haversine is usually faster than using a system/app-provided one, although this is on Windows where COM overhead can be "problematic" as it were!
The Haversine (and many similar geometry-on-the-Earth) formulae can be found here:
http://williams.best.vwh.net/avform.htm
(this is one of my few bookmarks I go back to for reference on a regular basis!)
for example user is at x=0,y=0
Calculate the distance by doing : SquareRoot(x2^2 & y2^2)
You will get a distance. Look through all points and compare which distance is the shortest

Calculate nearest point of KML polygon for iPhone app

I have a series of nature reserves that need to be plotted, as polygon overlays, on a map using the coordinates contained within KML data. I’ve found a tutorial on the Apple website for displaying KML overlays on map instances.
The problem is that the reserves vary in size greatly - from a small pond right up to several hundred kilometers in size. As a result I can’t use the coordinates of the center point to find the nearest reserves. Instead I need to calculate the nearest point of the reserves polygon to find the nearest one. With the data in KML - how would I go about trying to achieve this?
I've only managed to find one other person ask this and no one had replied :(
Well, there are a couple different solutions depending on your needs. The higher the accuracy required, the more work required. I like Phil's meanRadius parameter idea. That would give you a rough idea of which polygon is closest and would be pretty easy to calculate. This idea works best if the polygons are "circlish". If the polygon are very irregular in shape, this idea loses it's accuracy.
From a math standpoint, here is what you want to do. Loop through all points of all polygons. Calculate the distance from those points to your current coordinate. Then just keep track of which one is closest. There is one final wrinkle. Imagine a two points making a line segment that is very long. You are located one meter away from the midpoint of the line. Well, the distance to these two points is very large, while, in fact you are very close to the polygon. You will need to calculate the distance from your coordinate to every possible line segment which you can do in a variety of manners which are outlined here:
http://www.worsleyschool.net/science/files/linepoint/distance.html
Finally, you need to ask yourself, am I in any polygons? If you're 10 meters away from a point on a polygon, but are, in fact, inside the polygon, obviously, you need to consider that. The best way to do that is to use a ray casting algorithm:
http://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm