使用span缩放到用户位置

时间:2012-07-30 22:15:06

标签: iphone ios zoom cllocationmanager html

这是我现在的代码。我从位置管理器获得回调,但它不想缩放到该位置。

#import "MapViewController.h"


@interface MapViewController ()

@property (nonatomic, strong) CLLocationManager *locationManager;


@end

@implementation MapViewController

@synthesize mapView = _mapView;
@synthesize mPlacemark = _mPlacemark;
@synthesize location = _location;
@synthesize mStoreLocationButton = _mStoreLocationButton;
@synthesize locationManager = _locationManager;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    // Custom initialization
}
return self;
 }

- (void)viewDidLoad {

[super viewDidLoad];
mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
//mapView.showsUserLocation=TRUE;
mapView.delegate=self;
[self.view insertSubview:mapView atIndex:0];

NSLog(@"locationServicesEnabled: %@", [CLLocationManager locationServicesEnabled] ? @"YES":@"NO");
if ([self locationManager] == nil) {
    CLLocationManager *newLocationManager = [[CLLocationManager alloc] init];
    [newLocationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [newLocationManager setDistanceFilter:kCLDistanceFilterNone];
    [self setLocationManager:newLocationManager];
}

[[self locationManager] setDelegate:self];
[[self locationManager] startUpdatingLocation];
NSLog(@"Started updating Location");

}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

NSLog(@"Did update to location");
mStoreLocationButton.hidden=FALSE;
location=newLocation.coordinate;

MKCoordinateRegion region;
region.center=location;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;


[mapView setRegion:region animated:TRUE];


 }

如果需要,我可以发帖头文件。主要是我只关心缩放。我只是不明白为什么它不起作用。

1 个答案:

答案 0 :(得分:1)

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation的代码似乎是正确的。

尝试更改行

[self.view insertSubview:mapView atIndex:0];

为:

[self.view addSubview:mapView];

MKMapView可能因为它在视图中的显示方式而没有重绘自己。

相关问题