Rotate GMSMarker in direction at which user facing

前端 未结 8 1176
独厮守ぢ
独厮守ぢ 2020-12-28 17:58

I have requirement like on my current location one view will display. It will rotate if device was rotate or location will be change.I research lot but got all the code whic

相关标签:
8条回答
  • 2020-12-28 18:53

    Here the code modified with the changes suggested by Oren Trutner and from me:

    You just need to pass old location and new location. By passing required data you will get rotation value which is in float.

    #define degreesToRadians(x) (M_PI * x / 180.0)
    #define radiansToDegrees(x) (x * 180.0 / M_PI)
    
    - (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
    {
        float fLat = degreesToRadians(fromLoc.latitude);
        float fLng = degreesToRadians(fromLoc.longitude);
        float tLat = degreesToRadians(toLoc.latitude);
        float tLng = degreesToRadians(toLoc.longitude);
    
        float degree = radiansToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));
    
        if (degree >= 0) {
            return degree;
        } else {
            return 360+degree;
        }
    }
    
    0 讨论(0)
  • 2020-12-28 18:54

    Current location 'CLLocation' object has a property called 'course'

    @property(readonly, nonatomic) CLLocationDirection course;
    

    of type CLLocationDirection(typedef of double) which is an angle of the location.

    For car to rotate, You need extra field in your backend, direction, along with latitude and longitude. Use this information to rotate car by applying Transform on UIView

    CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);
    
    0 讨论(0)
提交回复
热议问题