无法在iPhone上获得GPS位置

时间:2020-12-27 23:32:17

My app runs perfectly on the simulator and everything works fine. But now i wanted to thest it on my iPhone and i found out that the GPS funcution don't work.

我的应用程序在模拟器上运行完美,一切正常。但现在我想在我的iPhone上找到它,我发现GPS功能不起作用。

Here is my code.

这是我的代码。

    [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];
}

And till now everything is fine. If i use NSLog i get the right coordinates.

到现在为止一切都很好。如果我使用NSLog,我会得到正确的坐标。

But then i want to use my coordinates here:

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

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

And there I get everytime 0 for lat1 and lat2. But only if I Run the App on the iPhone. On the Simulator it works fine.

对于lat1和lat2,我每次都得到0。但只有我在iPhone上运行应用程序。在模拟器上它工作正常。

Does someone know why this could be?

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

1 个解决方案

#1


1  

The problem is that you did not get yet a GPS location in real world, so your method connectionDidFinishLoading() is called before didUpdateToLocation() was called.
Therefoe eighValue is still 0.

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

Check first for valid lat and longitude:
if both are 0 then you did not get a location.

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

I think that you must change the logic of your program code,
you have to either

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

1) wait till you have a location, and then start the
connection such that connectionDidFinishLoading is called after you have a GPS coordinate.

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

Or 2) you store the coordinate result of the network connection, and calculate when you got your first coordinate

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

#1


1  

The problem is that you did not get yet a GPS location in real world, so your method connectionDidFinishLoading() is called before didUpdateToLocation() was called.
Therefoe eighValue is still 0.

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

Check first for valid lat and longitude:
if both are 0 then you did not get a location.

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

I think that you must change the logic of your program code,
you have to either

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

1) wait till you have a location, and then start the
connection such that connectionDidFinishLoading is called after you have a GPS coordinate.

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

Or 2) you store the coordinate result of the network connection, and calculate when you got your first coordinate

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