如何在不经常更新的情况下将MKMapView集中在用户身上

时间:2012-10-16 16:24:32

标签: ios5 center mapkit

我一直在搞乱mapkit,我有一个MKMapView,我设置它来显示使用位置。我想做的是,将地图置于用户位置的中心,而不是经常,而是一次(加载地图视图时)。我认为这是一个相当典型的用例,但我找不到简单的简洁解决方案。

2 个答案:

答案 0 :(得分:0)

我使用了开发人员库中的IOS Breadcrumb示例代码,Heres是“BreadCrumbViewController.m”中的部分

- (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];
            }
        }
    }
}

}

尝试并解析这个,你可能也喜欢这里的其他功能。

答案 1 :(得分:0)

@interface YourClass ()
@property (nonatomic, assign) BOOL didUpdateUserLocation;
@end

@implementation YourClass

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.didUpdateUserLocation = NO;

    self.mapView.delegate = self;
    self.mapView.showUserLocation = YES;
}

...

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    if (!self.didUpdateUserLocation) {

        // Set necessary span
        MKCoordinateSpan newSpan = MKCoordinateSpanMake(10, 10);
        MKCoordinateRegion newRegion = MKCoordinateRegionMake(userLocation.coordinate, newSpan);
        [self.mapView setRegion:newRegion animated:NO];

        self.didUpdateUserLocation = YES;
    }    
}

@end