iPhone - 中心地图,直到它完全准确

时间:2011-04-13 22:24:09

标签: ios objective-c mapkit

我正在创建一个应用程序,当打开它时,我开始获得用户位置并将地图居中到用户位置:

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

    [locationManager startUpdatingLocation];

然后我

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

    location = newLocation.coordinate;

    if (isOpening) {

        //Center location and set zoom on user when opening the app
        MKCoordinateRegion region;
        region.center = location;

        //Set Zoom level using Span
        MKCoordinateSpan span;
        span.latitudeDelta = .005;
        span.longitudeDelta = .005;
        region.span = span;

        isOpening = NO;

        [mapView setRegion:region animated:TRUE]; 
    }


}

使用isOpening标志,地图在完全准确之前停止居中,但如果我没有设置标志,则地图会一遍又一遍地居中。有没有办法以完全准确的方式获取位置并使地图居中,然后停止居中?

1 个答案:

答案 0 :(得分:0)

我是这样做的:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    NSTimeInterval locationAge = -[userLocation.location.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;
    if (userLocation.location.horizontalAccuracy < 0) return;

    CLLocation *location = userLocation.location;
    static BOOL found = NO;
    if (location && !found) {
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location.coordinate,1000,1000);
        [ridesMap setRegion:region animated:YES];
        found = YES;
    }//end if

}//end
相关问题