Topic: Find direction of geographic location relative to heading.

Hi,

How would I calculate the direction of a geographic lat/lon position, relative to the current heading / compass value?

Ie, if we’re facing East (trueHeading = 90.0) then a point to the east would have a bearing of 0 (ie, we're pointing directly at it), a point directly South would have a bearing 90 and I guess something to the North would have a bearing of -90 (or 270?)

Basically I want to be able to show if we’re ‘facing’ a certain point (The closer to zero, the more we’re facing it. 180 means we’re facing directly away from it)

Hope that makes sense!

Re: Find direction of geographic location relative to heading.

Hello.

Something like that:

using UnityEngine;

public class GetPointHeading:MonoBehaviour
{
    private OnlineMaps map;
    private float trueHeading;

    private void Start()
    {
        map = OnlineMaps.instance;
 
        OnlineMapsControlBase.instance.OnMapClick += OnMapClick;
        OnlineMapsLocationService.instance.OnCompassChanged += OnCompassChanged;
    }

    private void OnCompassChanged(float f)
    {
        trueHeading = f * 360;
    }

    private void OnMapClick()
    {
        double mx, my, px, py;
        map.GetPosition(out mx, out my);
        OnlineMapsControlBase.instance.GetCoords(out px, out py);

        // Online Maps v2.4 way:
        map.projection.CoordinatesToTile(mx, my, map.zoom, out mx, out my);
        map.projection.CoordinatesToTile(px, py, map.zoom, out px, out py);

        // Online Maps v2.3 way:
        // OnlineMapsUtils.LatLongToTiled(mx, my, map.zoom, out mx, out my);
        // OnlineMapsUtils.LatLongToTiled(px, py, map.zoom, out px, out py);

        // 0 - North, 90 - East, 180 - South, 270 (in this case, the following number is -89) - West
        float heading = (float)OnlineMapsUtils.Angle2D(mx, my, px, py) + 90;

        float relativeHeading = heading - trueHeading;
        if (relativeHeading < -180) relativeHeading += 360;
        else if (relativeHeading > 180) relativeHeading -= 360;

        Debug.Log("Heading: " + heading + ", True Heading: " + trueHeading + ", Relative Heading: " + relativeHeading);
    }
}
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Find direction of geographic location relative to heading.

hello please help me ,
How can i find my root path of two different location ?
i'hv go through the code ,but i can get it in last two days.

Thanks in advance

Re: Find direction of geographic location relative to heading.

Hello.

What is «root path of two different location»?
Is a route between two locations, or something else?

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.