MapKit缩放问题

时间:2012-04-04 22:11:01

标签: ios mapkit cllocationmanager

我有一个应用程序,其中包含一个带有针脚位置的地图,但我想更改它以便放大用户位置。

默认情况下,它从坐标0,0(非洲海岸附近)开始,但现在它在英国居住,但我不能让它放大到街道水平。当我调整Delta设置时,它没有任何区别。

这是我正在使用的代码。

#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

1 个答案:

答案 0 :(得分:0)

viewDidLoad中,此行不执行任何操作:

[[CLLocationManager alloc] init];

编译器应该在该行上发出诸如“表达结果未使用”之类的警告。


您需要实际将该代码的结果分配给locationManager变量,并且需要设置delegate否则不会调用委托方法:

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


顺便说一句,在viewDidLoad中,您要拨打[self addAnnotations];两次,我认为最好先调用[super viewDidLoad]而不是最后一次。