Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在objective c中获得经纬度的当前位置_Iphone_Objective C_Core Location_Latitude Longitude - Fatal编程技术网

Iphone 如何在objective c中获得经纬度的当前位置

Iphone 如何在objective c中获得经纬度的当前位置,iphone,objective-c,core-location,latitude-longitude,Iphone,Objective C,Core Location,Latitude Longitude,我正在使用以下代码获取当前位置,我添加了corelocation框架 - (void)viewDidLoad { [super viewDidLoad]; locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we

我正在使用以下代码获取当前位置,我添加了corelocation框架

- (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”。如何获得准确的纬度和经度。 我犯了错误,或者我需要添加任何其他方法、函数或框架


提前感谢。

在真实设备上测试应用程序。模拟器将始终显示纬度和经度的默认值。另外,请确保在您的设备上启用了位置服务。

您需要在设备上进行测试,以获取当前位置的纬度和经度,我猜您正在获取默认的Cupertino纬度和经度

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

您可以在需要获取当前纬度和经度的位置添加此方法:

-(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);

首先,您应该在模拟器中启用位置服务。调试->位置。大多数情况下,您将获得Cupertino的lat和long值。您可以通过选择第二个选项(自定义位置)为该选项设置自定义位置。当我们在调试区域选择运行时的位置时,这些选项将自动触发。

您在真实设备或模拟器上进行了测试吗?您在设备上启用了位置服务吗?@XSlash我在模拟器上进行了测试。@H2CO3我没有检查您在XCode上启用位置模拟器的设备ID?运行您的应用程序,然后它会appear@XSlash,在运行我的应用程序后,我只检查了它,但它没有显示u所显示的任何图标。@shasha转到
产品
->
编辑方案
->
选项
->选择
允许位置模拟
,然后你会得到图标X Slash,即使启用该选项,您也无法获得纬度和经度。当我尝试直接访问它们时,我会有一种奇怪的行为,即
坐标
s为(null)。还有印象,如果代码有时间,坐标就会被传递。似乎是时间问题。这返回0.000000表示纬度和经度。我正在使用Xcode 6.1.1和iPhone 5s模拟器。为什么呢?非常感谢你发布这个答案。对初学者来说真的很有帮助。这会每次返回0.000000的纬度和经度,即使在模拟器上也不会有问题。在调试按钮旁边的调试区域内,您可以选择不同的区域。在测试之前,您可以选择一些默认位置。或者你自己创造!