iOS:从没有地图的当前位置获取地理位置的角度

时间:2023-02-05 20:12:30

I'm working on some iPhone app and I want to make a bearing, an image that move to a specific geo location, depending on user location using accelerometer.

我正在研究一些iPhone应用程序,我想制作一个轴承,一个移动到特定地理位置的图像,具体取决于使用加速度计的用户位置。

I read many answers here but didn't get a solution.

我在这里读了很多答案,但没有得到解决方案。

I have the current location coordinates and destination ones.

我有当前的位置坐标和目的地坐标。

Have you any idea or sample code? Thanks.

您有任何想法或示例代码吗?谢谢。

1 个解决方案

#1


9  

on Top define this

在顶部定义这个

#define RadiansToDegrees(radians)(radians * 180.0/M_PI)
#define DegreesToRadians(degrees)(degrees * M_PI / 180.0)

define variable in .h file

在.h文件中定义变量

float GeoAngle;

in your location manager's delegate method :-

在您的位置管理器的委托方法中: -

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    GeoAngle = [self setLatLonForDistanceAndAngle:newLocation];
}

And the function will be as follows:-

功能如下: -

-(float)setLatLonForDistanceAndAngle:(CLLocation *)userlocation
{
    float lat1 = DegreesToRadians(userlocation.coordinate.latitude);
    float lon1 = DegreesToRadians(userlocation.coordinate.longitude);

    float lat2 = DegreesToRadians(locLat);
    float lon2 = DegreesToRadians(locLon);

    float dLon = lon2 - lon1;

    float y = sin(dLon) * cos(lat2);
    float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
    float radiansBearing = atan2(y, x);
    if(radiansBearing < 0.0)
    {
        radiansBearing += 2*M_PI;
    }

    return radiansBearing;
}

You will get constantly updated angle in GeoAngle variable. to rotate the arrow to destination location take the image of arrow and assign it to IBOutlet of arrowImageView and rotate this on heading update method.

您将在GeoAngle变量中获得不断更新的角度。要将箭头旋转到目标位置,请使用箭头图像并将其指定给arrowImageView的IBOutlet,并在标题更新方法上将其旋转。

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{
    float direction = -newHeading.trueHeading;

    arrowImageView.transform=CGAffineTransformMakeRotation((direction* M_PI / 180)+ GeoAngle);
}

#1


9  

on Top define this

在顶部定义这个

#define RadiansToDegrees(radians)(radians * 180.0/M_PI)
#define DegreesToRadians(degrees)(degrees * M_PI / 180.0)

define variable in .h file

在.h文件中定义变量

float GeoAngle;

in your location manager's delegate method :-

在您的位置管理器的委托方法中: -

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    GeoAngle = [self setLatLonForDistanceAndAngle:newLocation];
}

And the function will be as follows:-

功能如下: -

-(float)setLatLonForDistanceAndAngle:(CLLocation *)userlocation
{
    float lat1 = DegreesToRadians(userlocation.coordinate.latitude);
    float lon1 = DegreesToRadians(userlocation.coordinate.longitude);

    float lat2 = DegreesToRadians(locLat);
    float lon2 = DegreesToRadians(locLon);

    float dLon = lon2 - lon1;

    float y = sin(dLon) * cos(lat2);
    float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
    float radiansBearing = atan2(y, x);
    if(radiansBearing < 0.0)
    {
        radiansBearing += 2*M_PI;
    }

    return radiansBearing;
}

You will get constantly updated angle in GeoAngle variable. to rotate the arrow to destination location take the image of arrow and assign it to IBOutlet of arrowImageView and rotate this on heading update method.

您将在GeoAngle变量中获得不断更新的角度。要将箭头旋转到目标位置,请使用箭头图像并将其指定给arrowImageView的IBOutlet,并在标题更新方法上将其旋转。

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{
    float direction = -newHeading.trueHeading;

    arrowImageView.transform=CGAffineTransformMakeRotation((direction* M_PI / 180)+ GeoAngle);
}