-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,386
  • 3
  • 30
  • 47

1 Answers1

0

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
  • 415
  • 4
  • 12