Can the iPhone determine if you're facing north, south, east or west?

前端 未结 5 1163
梦毁少年i
梦毁少年i 2021-01-03 09:25

Can the iPhone determine if you\'re facing north, south, east or west?

5条回答
  •  梦毁少年i
    2021-01-03 10:05

    This maybe a bit of a hack but what I was doing was keeping tracking of the lat/long and then determining by that the direction.

    Within my code that returns the GPS information I created a delegate that would return back the latitude and longitude

    [[self delegate] currentDirectionLat:newLocation.coordinate.latitude Long:newLocation.coordinate.longitude];
    

    The code that was consuming the delegate would then have a method like so to get the coordinates and then set a label on the screen to the direction.

    -(void)currentDirectionLat:(float)latitude Long:(float)longitude
    {
        prevLongitude = currentLongitude;
        prevLatitude  = currentLatitude;
    
        currentLongitude = longitude;
        currentLatitude = latitude;
    
        NSString *latDirection = @"";
        if (currentLatitude > prevLatitude)
            latDirection = @"N";
        else if (currentLatitude < prevLatitude)
            latDirection = @"S";
    
        NSString *longDirection = @"";
        if (currentLongitude > prevLongitude)
            longDirection = @"E";
        else if (currentLongitude < prevLongitude)
            longDirection = @"W";
    
        if ([longDirection length] > 0)
            [lblDirection setText:[NSString stringWithFormat:@"%@%@",latDirection,longDirection]];  
    }
    

    I've not 100% tested this out but seems to have worked on my one application I was writing. If you want more information please let me know and I can forward you my code that I've done on it.

    You will also not get a good reading until the iPhone is moving to get the lat and long change. However, this does not take much to get it to change!

提交回复
热议问题