Get user current location on MKMapView

12,203

Solution 1

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

Try use self.userCurrentLocation = newLocation; instead.

Solution 2

- (void)viewDidLoad {
    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,46,320,370)];
    [mapView setDelegate:self];
    [mapView setShowsUserLocation:TRUE];    
    [self.view addSubview:mapView];
    [mapView setMapType:MKMapTypeStandard];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    if(locationManager.locationServicesEnabled)
    {
        [locationManager startUpdatingLocation];
    }
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    LatNLon = newLocation.coordinate;
    NSLog(@"%f   %f",LatNLon.latitude,LatNLon.longitude);
    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:LatNLon];
    [geocoder setDelegate:self];
    [geocoder start];
    isVisit=0;
    [locationManager stopUpdatingLocation];
}   
Share:
12,203
Gavin
Author by

Gavin

Updated on June 04, 2022

Comments

  • Gavin
    Gavin almost 2 years

    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];
    }
    
  • Gavin
    Gavin almost 13 years
    I have already did what you did in viewDidLoad. As for the reverse geocoding, I am using another class(SVGeocoder) apart from Apple's MKReverseGeoder. All works out fine, just that it seems that reading the userCurrentLocation object inside the IBAction crashes the app and I can't figured out why.
  • Rahul Juyal
    Rahul Juyal almost 13 years
    i think you are not getting current user location.that's why application is crash.
  • brandonscript
    brandonscript over 9 years
    Recommend you expanded this answer to explain a little better why and how this answers the question.