我在我的应用程序中遇到一个问题,我想知道当前Lat的纬度和经度方向;长
-(void)showDirection
{
CGFloat latitude = Lat;
if (latitude < 0) {
latitude = -latitude;
strDirection = @"S";
} else {
strDirection = @"N";
}
// Longitude
CGFloat longitude = Long;
if (longitude < 0) {
longitude = -longitude;
strDirection = @"W";
} else {
strDirection = @"E";
}
NSLog(@"direc %@",strDirection);
}
答案 0 :(得分:7)
尝试以下代码:
在添加初始化CLLocationManager对象并导入CLLocationManager头文件后,首先在项目 CoreLocation.Framework 中添加框架。
Yourviewcontroller.h
#import <CoreLocation/CoreLocation.h>
@interface Yourviewcontroller :UIViewController <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
Yourviewcontroller.m
locationManager=[[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
//Start the compass updates.
[locationManager startUpdatingHeading];
添加获取当前方向的函数
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
float mHeading = newHeading.magneticHeading;
if ((mHeading >= 339) || (mHeading <= 22)) {
//[direction setText:@"N"];
}else if ((mHeading > 23) && (mHeading <= 68)) {
//[direction setText:@"NE"];
}else if ((mHeading > 69) && (mHeading <= 113)) {
//[direction setText:@"E"];
}else if ((mHeading > 114) && (mHeading <= 158)) {
//[direction setText:@"SE"];
}else if ((mHeading > 159) && (mHeading <= 203)) {
//[direction setText:@"S"];
}else if ((mHeading > 204) && (mHeading <= 248)) {
//[direction setText:@"SW"];
}else if ((mHeading > 249) && (mHeading <= 293)) {
// [direction setText:@"W"];
}else if ((mHeading > 294) && (mHeading <= 338)) {
// [direction setText:@"NW"];
}
}
注意:指南针只能用于真正的iphone设备,不适用于iphone模拟器..!
答案 1 :(得分:-4)