我使用下面的代码来获取当前位置,我已经添加了corelocation框架如何获得当前位置的经度和纬度的目标C
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
NSLog(@" Current Latitude : %@",lat);
//latLabel.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
NSLog(@" Current Longitude : %@",longt);
//longLabel.text = longt;
}
但我得到的默认值,即,纬度:42°17'36.8898“和经度:-71°27'43.1003” 其中我当前的纬度:N 12°56'11.5624“和经度:E 77°32'41.0834”。如何获得确切的经纬度。 我在做错误,或者我需要添加任何其他方法或功能或框架。
在此先感谢。
你在真实设备或模拟器上测试过吗? –
您是否在设备上启用了位置服务? – 2012-01-25 06:11:49
@XSlash我在模拟器上测试过。 – shasha