使用当前位置缩放MapView的问题

时间:2012-03-30 08:00:07

标签: ios ios5 mkmapview cllocationmanager

我在应用程序的首页上显示地图视图,我使用CLLocation从当前位置设置区域和坐标。 运行应用程序时,地图视图上会显示蓝屏,当我打开其子视图并返回主屏幕时,地图会显示位置和缩放级别。 以下代码中是否有任何错误导致蓝屏?

- (void)viewWillAppear:(BOOL)animated {  
    CLLocationManager *lm = [[CLLocationManager alloc] init];
    lm.delegate = self;
    lm.desiredAccuracy = kCLLocationAccuracyBest;
    lm.distanceFilter = kCLDistanceFilterNone; 
    [lm startUpdatingLocation];

    CLLocation *location = [lm location];

    CLLocationCoordinate2D coord;


    coord = [location coordinate];

    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = coord.latitude; //39.281516;
    zoomLocation.longitude= coord.longitude; //-76.580806;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);

    MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];                

    [self.mainMapView setRegion:adjustedRegion animated:YES];      
}

1 个答案:

答案 0 :(得分:4)

我的猜测是你看到蓝色,因为位置是(0,0),它位于海中。可能是因为第一轮,CLLocationManager上的位置尚未设置。

您应该检查是否有位置,如果没有,请等待CLLocationManagerDelegate方法locationManager:didUpdateToLocation:fromLocation:中的回调,然后将地图置于您想要的位置。

这样的事情:

...
@property (nonatomic, assign) BOOL needsCentering
...

@synthesize needsCentering = _needsCentering;

- (void)viewWillAppear:(BOOL)animated {  
    CLLocationManager *lm = [[CLLocationManager alloc] init];
    lm.delegate = self;
    lm.desiredAccuracy = kCLLocationAccuracyBest;
    lm.distanceFilter = kCLDistanceFilterNone; 
    [lm startUpdatingLocation];

    CLLocation *location = [lm location];

    if (!location) {
        _needsCentering = YES;
    } else {
        _needsCentering = NO;

        CLLocationCoordinate2D coord;

        coord = [location coordinate];

        CLLocationCoordinate2D zoomLocation;
        zoomLocation.latitude = coord.latitude; //39.281516;
        zoomLocation.longitude= coord.longitude; //-76.580806;

        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);

        MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];                

        [self.mainMapView setRegion:adjustedRegion animated:YES];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if (_needsCentering) {
        _needsCentering = NO;

        CLLocationCoordinate2D coord;

        coord = [newLocation coordinate];

        CLLocationCoordinate2D zoomLocation;
        zoomLocation.latitude = coord.latitude; //39.281516;
        zoomLocation.longitude= coord.longitude; //-76.580806;

        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);

        MKCoordinateRegion adjustedRegion = [self.mainMapView regionThatFits:viewRegion];                

        [self.mainMapView setRegion:adjustedRegion animated:YES];
    }
}

但是,您也可以查看showsUserLocation的{​​{1}}。