Not Zooming at start


#1

I’m starting out the whereami project and when it builds (without error), get a map of the world. The blue dot with my location (on the device) or in Cupertino (on the simulator) isn’t on the screen but I can scroll to see it – so the zoom and center isn’t occurring when the annotation blue pin is placed.

I put an nslog statement in that method to say “got to didAddAnnotationViews” and nothing appears in the console so it isn’t being executed and I don’t know why.

No build errors.
I did check the delegation – the referencing outlets of delegate go to file owner, mapview, and the round button.

I get a console message: User location view is NOT visible but should be. Showing…

The code I have is:

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {

    // Create location manager object
    locationManager = [[CLLocationManager alloc] init];

    // Make this instance of location manager the delegate
    // that will receive the location messages
    [locationManager setDelegate:self];

    // We want all results from the location manager
    [locationManager setDistanceFilter:kCLDistanceFilterNone];

    // But we want location as accurate as possible …
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    // Start getting location info and show location on map
    [locationManager startUpdatingLocation];
    [mapView setShowsUserLocation:YES];

    [self.window makeKeyAndVisible];

    return YES;
    }

//implement the location did update – our instance method will create a log entry when method invoked

  • (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation
    {
    NSLog(@“Location: %@”, newLocation);
    }

// method to zoom into map location whenever an annotation is done
//

  • (void)mapView:(MKMapView *)mv didAddAnnotationViews: (NSArray *)views
    {NSLog(@“got to didAddAnnotationViews”);
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id mp = [annotationView annotation];
    MKCoordinateRegion region =
    MKCoordinateRegionMakeWithDistance([mp coordinate],250,250);
    [mv setRegion:region animated:YES];

}

Can someone point me in the right direction?


#2

check here: viewtopic.php?f=47&t=563&p=1506&hilit=didUpdateUserLocation#p1506


#3

That did it – thanks.


#4

The only problem with that method is that it gets continuously called and the map re-centers frequently. You can be scrolling around and a new UserLocation is updated and map resets. The best solution would be to have the code in didUpdateUserLocation: run once. I have tried using a simple (and global) BOOL set to YES when applicaiton:didFinishLaunching…: and then test it in didUpdateLocation: like this:

- (void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)userLocation { if(hasUserLocation == Yes){ MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.location.coordinate, 1000, 1000); [mv setRegion: region animated:YES]; hasUserLocation = NO; } }

Then the next problem occurs when an annotation is added and mapView needs to be reset around that. Haven’t figured that part out yet.

I will post if I get it before I get frustrated and move on.


#5

Should have waited… I figured it out.

[code]#import “WhereamiAppDelegate.h”
#import “MapPoint.h”

@implementation WhereamiAppDelegate

@synthesize window, switchMap;

BOOL hasUserLocation; // Here is bool for setting the zoom when didUpdateUser: is called first time

#pragma mark -
#pragma mark Application lifecycle

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate: self];
    [locationManager setDistanceFilter: 20];
    [locationManager setDesiredAccuracy: kCLLocationAccuracyBest];
    //[locationManager startUpdatingLocation];
    [mapView setShowsUserLocation: YES];
    [mapView setZoomEnabled: YES];

    switchMap = [[UISegmentedControl alloc] init];
    [switchMap addTarget: self action: @selector(mapStyleChanged:) forControlEvents: UIControlEventValueChanged];
    hasUserLocation = YES; // Set it here

    [self.window makeKeyAndVisible];

    return YES;
    }

  • (void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    if (hasUserLocation) // first time through it executes code
    {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.location.coordinate, 1000, 1000);
    [mv setRegion: region animated:YES];
    hasUserLocation = NO;
    }
    }

-(void) locationManager: (CLLocationManager *)manager didUpdateToLocation: (CLLocation *) newLocation fromLocation: (CLLocation *) oldLocation
{
NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];

if (t < -180)
	return;

		
MapPoint *mp = [[MapPoint alloc] initWithCoordinate: [newLocation coordinate] title: [locationTitleField text]];

MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate: [newLocation coordinate]];
[reverseGeocoder setDelegate: mp];
[reverseGeocoder start];

[mapView addAnnotation: mp];

// here is the mapView reset to zoom around added annotion
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1000, 1000);
[mapView setRegion: region animated:YES];

NSLog(@"%@", mp);
[mp release];
	
[self foundLocation];

}
[/code]

loacationManager:didUpdateToLocation:FromLoation: doesn’t get called until a new annotation is added. I reset the view here and it seems to be working.