MapView blue dot and circle


#1

Hi,
I managed to get the map working but I have one weird issue that I am not able to fix: the first time I get into the map tab, my location is found and map centered at my location with the blue dot and blue circle animated. However if I leave the map tab for let’s say the hypnosis tab and come back later it seems the MapView is not looking for my location anymore and the blue dot and circle are not animated anymore.

Has anyone encountered this issue?

PS: I have tried both creating the MapView programatically and using the Interface Builder with a XIB file.


#2

I’m not sure I see that as an issue. If you tap the blue dot you’ll see it start animating again.

I didn’t include the full code from the whereami project; I just showed the current location (the blue dot) rather than the full whereami functionality with annotations, etc. Are you including the full code, including the stuff from WhereAmIAppDelegate.m which calls [self findLocation], which in turn calls [locationManager startUpdatingLocation]? If not, then that’s why “MapView is not looking for [your] location anymore.”


#3

Thanks for you quick answer:
Below is the code of my view controller (MapView was added in IB) - I tried to keep it simple relatively to the features I wanted to implement. I thought the MapView didn’t need a separate CoreLocation object because the MapView is able to show user position without it.
What I have noticed is that mapView:didUpdateUserLocation is never called after I change tab and come back.

#import "WhereAmIViewController.h"


@implementation WhereAmIViewController
//@synthesize mapView;

- (id)init
{
    self = [super initWithNibName:nil bundle:nil];
    
    if(self)
    {
        UITabBarItem *tabBarItem = [self tabBarItem];
        
        [tabBarItem setTitle:@"Map"];
    }
    
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    return [self init];
}

- (void) viewDidLoad
{
    NSLog(@"WhereAmIViewController's view did load");
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"mapViewDidUpdateUserLocation");
    [(MKMapView*)self.view setRegion:MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250, 250) animated:YES];
}

- (void) dealloc
{
    [super dealloc];
}

@end

#4

[quote=“Radoubiwan”]Thanks for you quick answer:
Below is the code of my view controller (MapView was added in IB) - I tried to keep it simple relatively to the features I wanted to implement. I thought the MapView didn’t need a separate CoreLocation object because the MapView is able to show user position without it.
What I have noticed is that mapView:didUpdateUserLocation is never called after I change tab and come back.
[/quote]
Why would you expect mapView:didUpdateUserLocation to be called when you come back to the map tab?

Here’s my code. I made use of viewWillDisappear and viewDidAppear and turned setShowUserLocation off and on respectively in each so that I get the behavior you’re looking for.

#import "MyMapViewController.h"


@implementation MyMapViewController

- (id) init
{
    NSLog(@"Initializing MyMapViewController");
    
    self = [super initWithNibName:nil bundle:nil];
    
    if (self) {
        // Get the tab bar items
        UITabBarItem *tbi = [self tabBarItem];
        
        // Give it a lable
        [tbi setTitle:@"Location"];
        UIImage *i = [UIImage imageNamed:@"Location.png"];
        [tbi setImage:i];
        
        locationManager = [[CLLocationManager alloc]init];
        
        if (!locationManager) {
            NSLog(@"Couldn't create locationManager");
        }
        else {
            [locationManager setDelegate:self];                         // This is all fine and dandy but we start looking for a location before the view is even called....
            [locationManager setDistanceFilter:kCLDistanceFilterNone];  // ...shouldn't we wait to do all of this when someone taps on the view?
            [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; // So maybe we take advantage of viewWillAppear and viewHasDisappeared to turn location updating on and off?
        }
    }
    return self;
}

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil // I'm not entirely sure we need this...?
{
    // Disregard parameters - nib name is an implementation detail
    return [self init];
}

- (void) locationManager:(CLLocationManager *)manager 
     didUpdateToLocation:(CLLocation *)newLocation 
            fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"New Location: %@", newLocation);
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"Could not find location: %@", error);
}

- (void) viewDidAppear:(BOOL)animated
{
    NSLog(@"Entering MyMapViewController:viewDidAppear");
    
    [worldView setShowsUserLocation:YES]; 
}                                                         

- (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation // This is a method from the MKMapViewDelegate protocol. MKMapView has an attribute called "delegate" that will be assigned MyMapViewController. Then, whenever MKMapView updates its location it will call this method. It's here that we'll want to zoom in...right?
{
    NSLog(@"Entering mapView:didUpdateUserLocation");
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];
}

- (void) viewWillDisappear:(BOOL)animated
{
    NSLog(@"Entering viewWillDisappear");
    [worldView setShowsUserLocation:NO];
}