core location question

Discussion in 'iOS Programming' started by medasmx, Sep 26, 2010.

  1. medasmx macrumors member

    Nov 9, 2008
    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).

    - (void)viewDidLoad {    
        locationManager=[[CLLocationManager alloc]init];
    	[locationManager setDelegate:self];
    	[locationManager setDistanceFilter:kCLDistanceFilterNone];
    	[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    	[locationManager startUpdatingLocation];
    	[self.view setNeedsDisplay];
    #pragma mark -
    #pragma mark CLLocationManagerDelegateMethods
    - (void)locationManager:(CLLocationManager *)manager 
    	didUpdateToLocation:(CLLocation *)newLocation 
    		   fromLocation:(CLLocation *)oldLocation
    	if (startingPoint==nil) {
    	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.

  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
  3. medasmx thread starter macrumors member

    Nov 9, 2008
    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.

  4. ianray macrumors 6502

    Jun 22, 2010
    The gps takes time to settle. Wait until a location update arrives with sufficient horizontalAccuracy for your needs :)

Share This Page