Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

swift - Place annotations on a Map based on distance (meters) and bearing from current location

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...