Search code examples
iphoneobjective-ccllocation

autoupdate a distance between two locations in objective c


I'm very new to objective-c and have tried several sources to find an answer. No luck so far. Please help me out.

I'm building an app with location functionality. The app has to give an alert when a given distance is covered. Therefore I have to save a initial location. When moving the actual position has to be displayed andt the distance as well.

What I did is the following:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//display latitude
NSString *lat =[[NSString alloc]initWithFormat:@"%f",
                 newLocation.coordinate.latitude];
latitude.text=lat;
//latr = lat;

//display longitude
NSString *lon = [[NSString alloc]initWithFormat:@"%f",
                 newLocation.coordinate.longitude];
longitude.text=lon;
//longr = lon;

//display accuracy
//NSString *acc = [[NSString alloc]initWithFormat:@"%f",
//                 newLocation.horizontalAccuracy];
//accuracy.text=acc;
    }


-(void)recordLocation{

double valueLat = [[latitude text]doubleValue];
int latSeconds = valueLat * 3600;
int latDegrees = latSeconds / 3600;
latSeconds = abs(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;

char latChar = (latitude > 0) ? 'N' : 'S';

double valueLon = [[longitude text]doubleValue];
int lonSeconds = valueLon * 3600;
int lonDegrees = lonSeconds / 3600;
lonSeconds = abs(lonSeconds % 3600);
int lonMinutes = lonSeconds / 60;
lonSeconds %= 60;

char lonChar = (longitude > 0) ? 'E' : 'W';

CLLocation *tempPos = [[CLLocation alloc] initWithLatitude:valueLat longitude:valueLon];

NSString *recPosition = [[NSString alloc]initWithFormat:@"%i° %i' %i\" %c" "   " @"%i° %i' %i\"  %c \n", latDegrees, latMinutes, latSeconds, latChar,lonDegrees, lonMinutes, lonSeconds, lonChar];

_labelDegrees.text = recPosition;

NSLog(@"%f" , valueLat);
NSLog(@"%f" , valueLon);
NSLog (@"%@", tempPos);

[self distanceFromLocation:tempPos];

}

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)tempPos{

double lat1 = [[latitude text] doubleValue];
double lon1 = [[longitude text] doubleValue];

CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lon1];

CLLocationDistance locDistance = [currentLocation distanceFromLocation:tempPos];

NSString *distText = [[NSString alloc]initWithFormat:@"%f", locDistance];

NSLog(@"%@", currentLocation);
NSLog(@"%f", locDistance);
NSLog(@"%@", distText);

[lonDistance setText:distText];
}

It workes, but only one time, when I click a Button in my interface.There is no autoupdat of locDistance. The initial location updates automatic (found on the internet), the recording of this position is ok as well. What is wrong in my code. It is possible a simple issue but I'm not able to find the solution.

(Please forgive my poor English, I'm dutch)

Thank you in advance for your help

Regards,

Adri


Solution

  • You need to call recordLocation from within the didUpdateToLocation:fromLocation: method. That method is called automatically by the location manager whenever the location changes by more than the distanceFilter distance that can be set on the location manager. When it gets called, you want it to call recordLocation, which in turn calls distanceFromLocation.

    locationManager:didUpdateToLocation:fromLocation: is depreciated in iOS 6, so if you're using that iOS, then you should be using locationManager:didUpdateLocations: