MKMapView缩放到错误的区域

时间:2014-03-13 18:05:51

标签: mapkit cllocationmanager

我有三个视图是ViewController视图的子视图。三个标签切换各自的可见性。在“靠近我”功能中,我想只显示10英里范围内用户当前位置的项目。

在ViewController.h中

@property (weak, nonatomic) IBOutlet MKMapView *nearMeMapView;
@property (strong, nonatomic) CLLocationManager *locationManager;

在ViewController.m中

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.locationManager.delegate = self;
    self.nearMeMapView.delegate = self;
} 

- (IBAction)nearMeTap:(id)sender 
{
    self.nearMeMapView.hidden = NO;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    [self.locationManager startUpdatingLocation];
    NSLog(@"near me tapped")
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = newLocation.coordinate.latitude;
    zoomLocation.longitude= newLocation.coordinate.latitude;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 10*METERS_PER_MILE, 10*METERS_PER_MILE);
    [self.nearMeMapView setRegion:viewRegion animated:YES];
}

无论如何,问题是设备的位置被正确检测到但MapView缩放并动画到完全错误的位置。

1 个答案:

答案 0 :(得分:1)

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = newLocation.coordinate.latitude;
    //This was set wrong in your function
    zoomLocation.longitude= newLocation.coordinate.longitude;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 10*METERS_PER_MILE, 10*METERS_PER_MILE);
    [self.nearMeMapView setRegion:viewRegion animated:YES];
}

注释行是你的问题 您将zoom.longitude设置为纬度位置!