0

I'm hoping someone can point me to a convenient delegate method to answer the following question.

My question is: How do I know that the user has been located by MapKit for the first time?

When my view appears, I'd tell my MKMapView locate the user and set the map region around the user. After initially finding the user, I don't want to keep updating the map to his/her location. I want to give the user the freedom to pan around the map and not get automatically taken back to their location.

I'm having trouble when there's a delay in locating the user (for example, the very first time the user opens the app, MapKit doesn't locate the user until the he/she agrees to share their location). The result is that the map opens up somewhere over the Atlantic Ocean, and it doesn't correct itself once the user is found.

Unfortunately, -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation gets called a couple times before the user's real location has been determined (When testing, I see its NSLog statements before I ever agree to share my location).

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.mapView.delegate=self;
}

-(void)viewWillAppear:(BOOL)animated{
    [self.mapView setShowsUserLocation:YES];
    [self.mapView setUserTrackingMode: MKUserTrackingModeNone];

    hasUpdatedRegion=NO;
}


//MapKit Delegate Methods
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation");

    if(!hasUpdatedRegion)
    {
        hasUpdatedRegion=YES;
        MKCoordinateSpan span=MKCoordinateSpanMake(0.3, 0.3);
        MKCoordinateRegion currentRegion=MKCoordinateRegionMake(mapView.userLocation.coordinate, span);
        [mapView setRegion:currentRegion];
    }
}

-(void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated{
    NSLog(@"didChangeTrackingMode");

}
Ben Boral
  • 355
  • 3
  • 11
  • See [this](http://stackoverflow.com/questions/9680576/is-this-a-bug-with-mkmapkitdelegate-mapviewdidupdateuserlocation/9702830#9702830) and [this](http://stackoverflow.com/questions/9543571/how-to-check-validity-of-cllocation-in-ios). –  Jul 02 '12 at 13:31
  • Bingo! Thanks, Anna Karenina! If you want to answer this question with that info, I will accept your answer. – Ben Boral Jul 03 '12 at 02:22

2 Answers2

2

I have the same problem. Made another way...

- (void)viewDidLoad
{
    self.mapView.userTrackingMode = YES;
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    self.mapView.userTrackingMode = NO;
}

First, set userTrakingMode to YES track the user as s/he moves.

Second, after updating map to user location didUpdateUserLocation is fired, when I've disabled the tracking mode.

This stops the map from tracking the user any more.

Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
0

Why don't you , try setting the mapview region , so that the whole world map shows instead of a particular location(as u said 'Atantic ocean'), in case of user didn't allow the mapkit to use current location(i mean in view did load method ,set the map region .),,and if he allows..then set the region as per user's coordinates,.