core location question

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

  1. medasmx macrumors member

    Joined:
    Nov 9, 2008
    #1
    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
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
  3. medasmx thread starter macrumors member

    Joined:
    Nov 9, 2008
    #3
    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
     
  4. ianray macrumors 6502

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

Share This Page