Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user current location on MKMapView

I am trying to get the user current latest location on the didUpdateToLocation:newLocation:fromLocation:oldLocation: method and store it in my ivar CLLocation *userCurrentLocation

I get this error when I tried to read userCurrentLocation

-[NSCFNumber coordinate]: unrecognized selector sent to instance 0x586b220



- (void) locationManager:(CLLocationManager *) manager
     didUpdateToLocation:(CLLocation *) newLocation
            fromLocation:(CLLocation *) oldLocation {
            fromLocation:(CLLocation *) oldLocation {
if (oldLocation == nil) { // first time loading? 
        SVGeocoder *geocodeRequest = [[SVGeocoder alloc] 
                                      initWithCoordinate:CLLocationCoordinate2DMake(newLocation.coordinate.latitude,     newLocation.coordinate.longitude)];
         [geocodeRequest setDelegate:self];
        [geocodeRequest startAsynchronous]; // reverse geocoding to get annotation coordinates to be placed.
        [geocodeRequest release];
        isCurrentLocation = YES; //flag that next annotation placed is current location
    }
        userCurrentLocation = newLocation;
NSLog(@"didUpdateToLocation - Lat:%f Long:%f", userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude); // verified that userCurrentLocation latitude is correct at this point.
}

Toolbar button to show user current location

-(IBAction) showLocation:(id) sender {
    NSLog(@"Lat:%f Long:%f", userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude); // crashes upon calling this statement. Why?
    NSLog(@"Show Location Called");
    SVGeocoder *geocodeRequest = [[SVGeocoder alloc] 
                                  initWithCoordinate:CLLocationCoordinate2DMake(userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude)];
    [geocodeRequest setDelegate:self];
    [geocodeRequest startAsynchronous];
    [geocodeRequest release];

}

I did set the property and synthesize userCurrentLocation as such:

MapViewController.h

@interface MapViewController : UIViewController <CLLocationManagerDelegate,  MKMapViewDelegate> {
   CLLocation *userCurrentLocation;
}
@property (nonatomic, retain) CLLocation *userCurrentLocation;
@end

MapViewController.m

@synthesize userCurrentLocation;

          /* EDITED */
- (void)viewDidLoad {
    locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
    userCurrentLocation = [[CLLocation alloc] init];
}
like image 421
Gavin Avatar asked Aug 29 '11 07:08

Gavin


1 Answers

Your userCurrentLocation is autoreleased after the - (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation method finished.

Try use self.userCurrentLocation = newLocation; instead.

like image 94
xuzhe Avatar answered Nov 10 '22 00:11

xuzhe