-1

I'm working on an App to layout a sailing race course based on the number of boats, target time, wind direction and wind strength.

Currently, I store all variables on the device and Pin the race committee starting boat on the map. I found a lot of information on how to calculate the distance between two locations, but can't find any information on how to place an annotation xx meters/bearing xx degrees from the starting vessel.

The starting vessel is on the map (Location: latitude/longitude). How can i place an annotation 500 meters away from the starting vessel, bearing 90 degrees?

Kamran
  • 14,987
  • 4
  • 33
  • 51

1 Answers1

2

I believe there are a lot of example on how to put annotations on a lat/long location. So I will help a bit on how to get the target location

First you can see the Haversine formula to get the lat/long position there is a good example for python at Get lat/long given current point, distance and bearing

for swift you can use this function, assuming the target is 500 meter and 90 degree from the current user heading

   func getNewTargetCoordinate(position: CLLocationCoordinate2D, userBearing: Float, distance: Float)-> CLLocationCoordinate2D{

    let r = 6378140.0
    let latitude1 = position.latitude * (Double.pi/180) // change to radiant
    let longitude1 = position.longitude * (Double.pi/180)
    let brng = Double(userBearing+90) * (Double.pi/180)

    var latitude2 = asin(sin(latitude1)*cos(Double(distance)/r) + cos(latitude1)*sin(Double(distance)/r)*cos(brng));
    var longitude2 = longitude1 + atan2(sin(brng)*sin(Double(distance)/r)*cos(latitude1),cos(Double(distance)/r)-sin(latitude1)*sin(latitude2));

    latitude2 = latitude2 * (180/Double.pi)// change back to degree
    longitude2 = longitude2 * (180/Double.pi)

    // return target location
    return CLLocationCoordinate2DMake(latitude2, longitude2)
}
Darwin Harianto
  • 435
  • 4
  • 15
  • This is exactly what I was looking for. With this function, I can calculate the location for buoy 1, buoy 2, buoy 3, etc. from start by parsing in the variables distance and bearing. Thanks a lot for your help! – Valentijn van Duijvendijk Oct 16 '18 at 09:08
  • your welcome, if this solved your problem, you can make it as an accepted answer – Darwin Harianto Oct 17 '18 at 00:33
  • Looks like they never greenchecked you. I used the method in an extension. Once I have it tested, I'll maybe paste it here, but your answer is good. It does have the caveat that the Haversine formula does not take into account the "squashed" shape of the earth, but for shortt distances, it's probably the best. – Chris Marshall Dec 03 '22 at 20:49