2012-01-25 64 views
5

我使用下面的代码来获取当前位置,我已经添加了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”。如何获得确切的经纬度。 我在做错误,或者我需要添加任何其他方法或功能或框架。

在此先感谢。

+1

你在真实设备或模拟器上测试过吗? –

+0

您是否在设备上启用了位置服务? – 2012-01-25 06:11:49

+0

@XSlash我在模拟器上测试过。 – shasha

回答

2

在真实设备上测试应用程序。模拟器将一直向您显示经度和纬度的默认值。还要确保设备上的位置服务已启用。

+0

即使在模拟器上,它也不应该成为问题。在调试区域内可以看到调试按钮旁边,您可以选择不同的区域。有一些默认职位可供您选择前期测试。或者你创建自己的! –

1

你需要在设备上测试它以获取当前位置的经纬度,我猜你正在获取库比蒂诺的经度和纬度,这是默认值。

2

您必须在设备中测试位置服务,而不是模拟器,尽管您可以在模拟器上启用位置模拟,请参阅我的图像附件和圆圈图标。但它不会是你的确切位置!但至少你可以验证你的代码工作 Image

+0

我正在使用xcode 4,我不能看到它上面的圆圈图标 – shasha

+0

运行你的应用程序,然后它会出现 –

+0

@X斜杠,运行我的应用程序后,只有我检查了它,但它没有显示任何图标,因为你显示。 – shasha

11

,你需要获得当前的纬度和经度,您可以添加这个方法:

-(CLLocationCoordinate2D) getLocation{ 
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation]; 
    CLLocation *location = [locationManager location]; 
    CLLocationCoordinate2D coordinate = [location coordinate]; 

    return coordinate; 
} 

,并呼吁它使用:

CLLocationCoordinate2D coordinate = [self getLocation]; 
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude]; 

NSLog(@"*dLatitude : %@", latitude); 
NSLog(@"*dLongitude : %@",longitude); 
+1

我有奇怪的行为,当我尝试直接访问它们时,'coordinate's(null)。还有印象,如果代码有一段时间的坐标交付。看起来像是一个时间问题。 – jerik

+0

这将为经度和纬度返回0.000000。我正在使用Xcode 6.1.1和iPhone 5s模拟器。为什么这样? –

+0

非常感谢发布这个答案。真的很适合初学者。 –

0

首先您应该在模拟器中启用位置服务。调试 - >位置。大部分情况下,您将获得库比蒂诺的经济价值。您可以通过选择第二个选项(自定义位置)来为此选项设置自定义位置。当我们在Debugging区域中运行时选择位置时,这些选项会自动触发。

+0

谢谢....我挣扎了很多,找到它:) – vijeesh

相关问题