【问题标题】:Cannot get GPS location on iPhone无法在 iPhone 上获取 GPS 位置
【发布时间】:2013-03-08 19:08:48
【问题描述】:

我的应用在模拟器上运行完美,一切正常。但是现在我想在我的 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

在那里,lat1 和 lat2 每次都得到 0。但前提是我在 iPhone 上运行应用程序。在模拟器上运行良好。

有人知道为什么会这样吗?

【问题讨论】:

  • eigLat 在哪里定义? (不是 GPS 问题,是简单的编程问题,在 didUpdateToLocation 中记录输出 eigLat,在手机上也可以。)
  • 在.h文件中作为double
  • 为什么将数字存储为字符串?在connectionDidFinishLoading:中,data从何而来?
  • @Sebastian 这不是重点,问题是 lat1 / eigLat 为 0; (字符串来自 JSON)
  • 数据来自我的 JSON;我认为它更容易,因为我得到字符串,然后将其转换为双精度

标签: iphone cocoa-touch gps ios-simulator


【解决方案1】:

问题是您还没有在现实世界中获得 GPS 位置,所以 您的方法 connectionDidFinishLoading() 在调用 didUpdateToLocation() 之前被调用。
因此 eighValue 仍为 0。

首先检查有效的纬度和经度:
如果两者都是 0,那么您没有获得位置。

我认为你必须改变你的程序代码的逻辑,
你必须要么

1) 等到你有了位置,然后开始
连接,以便在您获得 GPS 坐标后调用 connectionDidFinishLoading。

或者 2)你存储网络连接的坐标结果,当你得到你的第一个坐标时计算

【讨论】:

  • 哦,天哪,我已经用断点检查过了,这就是问题所在。但是当我用模拟器运行程序时,我首先得到了位置。你知道我该如何解决这个问题吗?
  • 一种解决方案,等待连接,直到您获得 GPS 位置。你也可以在模拟器上模拟这个,如果你停止gps求和,稍后你启用它。
  • 好的,现在我已将 foor-loop 放在位置 mehtod 中。但是 porlbme 是,起初我只有 0m,当我在底部滚动然后回到顶部时,距离是正确的。你能帮我解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 2014-07-12
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多