MapKit zoom questions

Go To StackoverFlow.com

0

I've got an app that has a map with pins for locations, but I want to change it so zooms in on the users location.

By default it starting off at the coordinates 0,0 (off the coast of Africa) but now it homes in on the U.K. but I cannot get it to zoom in to street level. When I adjust the Delta settings it doesnt make any difference.

Here is the code I am using.

#import "FirstViewController.h"
#import "MapPin.h"

@implementation FirstViewController

@synthesize map;
@synthesize locationManager , location;


-(void)addAnnotations{

    // Normally read the data for these from the file system or a Web service
    CLLocationCoordinate2D coordinate = {53.0670, -2.521};
    MapPin *pin = [[MapPin alloc]initWithCoordinates:coordinate
                                           placeName:@"Mr Shoe - Gents,Ladies,Kids"
                                         description:@"01270 626767, 123 Your Street, CW5 5NA"
                   ];

    [self.map addAnnotation:pin];

    [pin release];

    // Normally read the data for these from the file system or a Web service
    CLLocationCoordinate2D coordinate2 = {53.0659, -2.521};
    MapPin *pin2 = [[MapPin alloc]initWithCoordinates:coordinate2
                                            placeName:@"Veg-is-us - Fruit & veg"
                                          description:@"01270 626767, 123 Your Street, CW5 5NA"

                    ];

    [self.map addAnnotation:pin2];


    [pin2 release]; 

    CLLocationCoordinate2D coordinate3= {53.06879, -2.52195};
    MapPin *pin3= [[MapPin alloc]initWithCoordinates:coordinate3
                                            placeName:@"PC Centre Nantwich"
                                          description:@"01270 626767, 15c Beam street, CW5 5NA"

                    ];

    [self.map addAnnotation:pin3];


    [pin3 release]; 
}

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 if (self) {
 // Custom initialization.
 }
 return self;
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    [self addAnnotations];
        [self addAnnotations];


     [super viewDidLoad];
}
-(void) locationManager: (CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    location = newLocation.coordinate;
    //One location is obtained.. just zoom to that location

    MKCoordinateRegion region;
    region.center=location;
    //Set Zoom level using Span
    MKCoordinateSpan span;
    span.latitudeDelta=0.4;
    span.longitudeDelta=0.4;
    region.span=span;
    NSLog(@"map=%@", map);
    [map setRegion:region animated:TRUE];

}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


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


@end
2012-04-04 22:11
by user1313926
anyone? it must be a simple mistake but I cannot see it - user1313926 2012-04-05 09:33
Code looks ok. Are you sure the delegate method is being called (put a breakpoint or NSLog there)? In that delegate method, right before the setRegion line, put NSLog(@"map=%@", map); and see what it prints. Are you updating the map region anywhere else in your app - NoName 2012-04-05 14:06
@MDT this is my code, I've tried to output to debug but it doesnt seem to log it since I upgraded Xcode (old debug console worked ok - user1313926 2012-04-05 16:05


0

In viewDidLoad, this line doesn't do anything:

[[CLLocationManager alloc] init];

The compiler should be giving a warning such as "Expression result unused" on that line.


You need to actually assign the result of that code to the locationManager variable and you need to set the delegate otherwise the delegate methods won't get called:

self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;


By the way, in viewDidLoad, you are calling [self addAnnotations]; twice and I think it's better to call [super viewDidLoad] first instead of last.

2012-04-05 16:31
by NoName
thanks @Anna I've tided it up a little and it and it now zooms in which is awesome, many thanks for saving me more head banging, however I have lost the ability now to navigate around the map, it rubber bands back to centre. Any ideas - user1313926 2012-04-05 21:14
The didUpdateToLocation method will keep getting called every time the user location updates and the code in there keeps re-centering on the user. To zoom in on just the first update, call [manager stopUpdatingLocation]; at the end of that method - NoName 2012-04-05 21:20
you are a saviour.. many thank - user1313926 2012-04-05 21:25
Ads