dereflo
dereflo

Reputation: 31

Cannot get GPS location on iPhone

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.

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.

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.

Does someone know why this could be?

Upvotes: 3

Views: 1906

Answers (1)

AlexWien
AlexWien

Reputation: 28727

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.

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

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.

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

Upvotes: 1

Related Questions