0

So I'm very very very very new to programming, but I've fumbled my way through this far: I'm creating an Iphone App, My goal is to create an automatic stopwatch; something that starts itself at a pre-set location and stops itself at a pre-set location ( both of which are set by the user). If the pre-set gps location and the actual gps location are equal, a timer will start/stop. I've done this by setting a location as a variable at the push of a button.

here's the problem: The two values are never equal. The pre-set start and finish locations contain many more decimals than the "actual" location ( the values themselves, not just the printed values) and if they are never equal, the timer will not start.

any help is appreciated!!!!

Felix Lamouroux
  • 7,414
  • 29
  • 46

1 Answers1

3

You could simply calculate the distance between the two coordinates and start/stop the timer when the distance drops below a certain threshold (e.g. 50m) to the start or end location.

CLLocation provides a convient method for doing so:

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

For example:

 double lat = 51.7;
 double lng = 7.0;
 CLLocation *locationStart = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
 CLLocation *currentLocation = ...; // received from location services

 if ([locationStart distanceFromLocation:currentLocation] < 50) {
     // start timer 
 } 

You may also consider using the horizontalAccuracy property of the current location instead of the arbitrary 50m. Beware however that the accuracy can be as bad as several kilometers. Something like MIN(horizontalAccuracy, 50) may work well.

Felix Lamouroux
  • 7,414
  • 29
  • 46
  • :what data type is usable with CLLocation? I've tried setting it equal to an integer, a float, and a double and it keeps rejecting the variable and saying it's the wrong format. – user1370133 May 04 '12 at 15:22
  • CLLocation is an object-class. To create an instance you need the latitude and longitude ideally as doubles (or CLLocationDegrees which is double). You can then create it with `[[CLLocation alloc] initWithLatitude:lat longitude:lng]` – Felix Lamouroux May 04 '12 at 15:50