1

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

#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];


 }

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

4

1 回答 1

1

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

换行试试

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

至:

[self.view addSubview:mapView];

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

于 2012-07-30T22:27:38.557 回答