Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

medasmx

macrumors member
Original poster
Nov 9, 2008
89
0
Can anyone let me know why the below code works on the simulator but not my iPhone? The idea is to specify a longitude and latitude (like home location), then when you press update (a button) it calculates how far you are from home (or whatever location you have inputted).

Code:
- (void)viewDidLoad {    
    locationManager=[[CLLocationManager alloc]init];
	[locationManager setDelegate:self];
	[locationManager setDistanceFilter:kCLDistanceFilterNone];
	[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
	[locationManager startUpdatingLocation];
}

-(IBAction)update:(id)sender
{
	[self.view setNeedsDisplay];
}

#pragma mark -
#pragma mark CLLocationManagerDelegateMethods
- (void)locationManager:(CLLocationManager *)manager 
	didUpdateToLocation:(CLLocation *)newLocation 
		   fromLocation:(CLLocation *)oldLocation
{
	if (startingPoint==nil) {
		self.startingPoint=newLocation;
	}
	
	CLLocation*myHome=[[CLLocation alloc]initWithLatitude:41.513546 longitude:-81.557974];
	
	CLLocationDistance theDistance=[startingPoint distanceFromLocation:myHome];
	distanceLabel.text=[[NSString alloc]initWithFormat:@"%gm",theDistance];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
	NSLog(@"could not find location: %@",error);
}

The error is the following--
"[CLLocation distanceFromLocation:]: unrecognized selector"...uncaught exception.

Any help is appreciated.

Adam
 
Thanks. That worked.

I switched to getDistanceFrom and it works fine. One other thing... It appears that the current location on my iPhone isn't exactly accurate. Is there any way to adjust that. In any case thanks for the suggestion.

Adam
 
One other thing... It appears that the current location on my iPhone isn't exactly accurate. Is there any way to adjust that.

The gps takes time to settle. Wait until a location update arrives with sufficient horizontalAccuracy for your needs :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.