Ios5 如何在不不断更新的情况下将MKMapView集中在用户上

Ios5 如何在不不断更新的情况下将MKMapView集中在用户上,ios5,center,mapkit,Ios5,Center,Mapkit,我一直在摆弄mapkit,我有一个MKMapView,我设置它来显示使用位置。我想做的是,将地图放在用户位置的中心,不是不断地,而是一次(当地图视图加载时)。我假设这是一个相当典型的用例,但我无法找到一个简单简洁的解决方案 我使用了开发人员库中的IOS面包屑示例代码,这是“面包屑导航控制器.m”中的部分 } 试着仔细分析一下,这里还有其他你可能喜欢的功能 @interface YourClass() @属性(非原子,赋值)BOOL didUpdateUserLocation; @结束 @类的实

我一直在摆弄mapkit,我有一个MKMapView,我设置它来显示使用位置。我想做的是,将地图放在用户位置的中心,不是不断地,而是一次(当地图视图加载时)。我假设这是一个相当典型的用例,但我无法找到一个简单简洁的解决方案

我使用了开发人员库中的IOS面包屑示例代码,这是“面包屑导航控制器.m”中的部分

}

试着仔细分析一下,这里还有其他你可能喜欢的功能

@interface YourClass()
@属性(非原子,赋值)BOOL didUpdateUserLocation;
@结束
@类的实现
-(无效)viewDidLoad
{
[超级视图下载];
self.didUpdateUserLocation=否;
self.mapView.delegate=self;
self.mapView.showUserLocation=是;
}
...
-(void)mapView:(MKMapView*)mapView didUpdateUserLocation:(MKUserLocation*)userLocation
{
如果(!self.didUpdateUserLocation){
//设置必要的跨度
MKCoordinateSpan newSpan=MKCoordinateSpanMake(10,10);
MKCoordinateRegion newRegion=MKCoordinateRegionMake(userLocation.coordinate,newSpan);
[self.mapView setRegion:newRegion动画:否];
self.didUpdateUserLocation=是;
}    
}
@结束
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{
if (newLocation)
{
    if ([self.toggleAudioButton isOn])
    {
        [self setSessionActiveWithMixing:YES]; // YES == duck if other audio is playing
        [self playSound];
    }

    // make sure the old and new coordinates are different
    if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
        (oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
    {    
        if (!self.crumbs)
        {
            // This is the first time we're getting a location update, so create
            // the CrumbPath and add it to the map.
            //
            _crumbs = [[CrumbPath alloc] initWithCenterCoordinate:newLocation.coordinate];
            [self.map addOverlay:self.crumbs];

            // On the first location update only, zoom map to user location
            MKCoordinateRegion region = 
                MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
            [self.map setRegion:region animated:YES];
        }
        else
        {
            // This is a subsequent location update.
            // If the crumbs MKOverlay model object determines that the current location has moved
            // far enough from the previous location, use the returned updateRect to redraw just
            // the changed area.
            //
            // note: iPhone 3G will locate you using the triangulation of the cell towers.
            // so you may experience spikes in location data (in small time intervals)
            // due to 3G tower triangulation.
            // 
            MKMapRect updateRect = [self.crumbs addCoordinate:newLocation.coordinate];

            if (!MKMapRectIsNull(updateRect))
            {
                // There is a non null update rect.
                // Compute the currently visible map zoom scale
                MKZoomScale currentZoomScale = (CGFloat)(self.map.bounds.size.width / self.map.visibleMapRect.size.width);
                // Find out the line width at this zoom scale and outset the updateRect by that amount
                CGFloat lineWidth = MKRoadWidthAtZoomScale(currentZoomScale);
                updateRect = MKMapRectInset(updateRect, -lineWidth, -lineWidth);
                // Ask the overlay view to update just the changed area.
                [self.crumbView setNeedsDisplayInMapRect:updateRect];
            }
        }
    }
}