Search code examples
iosmapscompass-geolocation

How to get the direction of a point in the map in iOS?


I need to mark a point in the map and get the direction of that point (like in a compass) related to the angle that the phone is currently being held in. Any tips would be highly appreciated. Thanks.


Solution

  • Your vector for the angle of the phone will be: av = (cos(phone_angle), sin(phone_angle))

    Your vector for the angle from the phone to the destination will be dv = (x_phone_location - x_dest, y_phone_location - y_dest)

    Find the magnitude (length) of each of the two above vectors. Call these magnitudes mag_dv and mag_av

    You can then do the dot product of the destination vector and the phone angle vector.

    angle = arccos( ((dv_x * av_x)+(dv_y * av_y)) / (mag_av * mag_dv) )