iPhone无法获取GPS位置

3

我的应用在模拟器上运行得非常完美,一切正常。但现在我想在我的iPhone上测试它,结果发现GPS功能不能使用。

下面是我的代码:

    [super viewDidLoad];

    //eigener Standort
    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
{
    eigLat = newLocation.coordinate.latitude;
    eigLon = newLocation.coordinate.longitude;

    eigLatString = [NSString stringWithFormat:@"%f", eigLat];
    eigLonString = [NSString stringWithFormat:@"%f", eigLon];
}

目前为止一切都很好。如果我使用NSLog,我能得到正确的坐标。

但是现在我想在这里使用我的坐标:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    news = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];

    for (x=0; x<[news count]; x++)
    {
        //Berechnung der Entfernung
        erdradius = 6371.1;

        //Koordinaten von Datenbank in String schreiben
        NSString *latitude = [[news objectAtIndex:x] objectForKey:@"Latitude"];
        NSString *longitude = [[news objectAtIndex:x] objectForKey:@"Longitude"];

        entfernung = 0;
        double lat1 = eigLat;                   //eigener Standort Latitude
        double long1 = eigLon;                  //eigener Standort Longitude
        double lat2 = [latitude doubleValue];   //Standort Heurigen Latitude
        double long2 = [longitude doubleValue]; //Standort Heurigen Longitude

每次在 iPhone 上运行应用程序时,lat1 和 lat2 的值都为 0。但是在模拟器上却可以正常工作。

有人知道这是为什么吗?


@Sebastian,这不是重点,问题在于lat1 / eigLat为0;(字符串来自JSON) - AlexWien
@dereflo 发布 eigLat 定义的代码和 Objective-C 类。 - AlexWien
@AlexWien:这可能不是问题所在。但是发布的代码是不完整的。提供一个简短、自包含、正确(可编译)的示例总是更好的选择。 - Sebastian
是的,使用JSON数据一切正常。只有GPS不工作。 - dereflo
你需要整个代码吗?因为这只是一些片段。 - dereflo
显示剩余10条评论
1个回答

1
问题在于您尚未在现实世界中获得GPS位置,因此在调用 方法didUpdateToLocation()之前,已经调用了你的方法 connectionDidFinishLoading()
因此,eightValue仍为0。
首先检查有效的纬度和经度:
如果两者都为0,则表示您没有获得位置。
我认为您必须更改程序代码的逻辑,要么 1)等待直到有一个位置,然后启动连接,以使在您获得GPS坐标后调用 connectionDidFinishLoading。

2) 存储网络连接的坐标结果,并计算何时获得第一个坐标。

好的,我已经使用断点检查过了,这就是问题所在。但是当我用模拟器运行程序时,我首先得到了位置信息。你有什么想法可以解决这个问题吗? - dereflo
一种解决方案是,在获得GPS位置之前等待连接。如果您停止GPS模拟,则可以在模拟器上进行模拟,稍后再启用它。 - AlexWien
好的,现在我已经将 for 循环放到位置方法中了。但问题是,刚开始我只有0m,当我向下滚动然后回到顶部时,距离才正确。你能帮我解决这个问题吗? - dereflo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接