How to rotate a Direction Arrow to particular location

前端 未结 4 468
谎友^
谎友^ 2020-12-02 19:32

In my app I have a latitude-longitude of 1 fix location. Now user with iPhone device can move anywhere and even he rotate his device, the arrow (some uiimageview) should poi

4条回答
  •  攒了一身酷
    2020-12-02 19:50

    Try this example. Its in C# Xamarin tho :) "double heading" is the CLHeading.MagneticHeading from the eventobject in CLLocationManager.UpdatedHeading.

    void UpdateCompass(Location origin, Location target, double heading)
    {
        var angle1 = GetAngleBetweenPoints(origin, target);
        var angle2 = GetAngleFromHeading(heading);
        var radian = Math.PI * (angle1 + angle2) / 180.0;
        CompassArrow.Transform = CGAffineTransform.MakeRotation((nfloat)radian);
    }
    
    double GetAngleBetweenPoints(Location origin, Location target)
    {
        var n = 270 - (Math.Atan2(origin.Latitude - target.Latitude, origin.Longitude - target.Longitude)) * 180 / Math.PI;
        return n % 360;
    }
    
    double GetAngleFromHeading(double heading)
    {
        var radians = -heading / 180.0 * Math.PI;
        return radians * (180.0 / Math.PI);
    }
    

提交回复
热议问题