0

私はmapkitをいじっています.MKMapViewがあり、使用場所を表示するように設定しています. 私がやりたいことは、常にではなく、一度(マップビューがロードされたとき)、ユーザーの場所にマップを中央に配置することです。これはかなり典型的な使用例だと思いましたが、簡単で簡潔な解決策を見つけることができません。

4

2 に答える 2

0

開発者ライブラリの IOS Breadcrumb サンプル コードを使用しました。「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];
            }
        }
    }
}

}

これを試して分析してください。ここには他にも好きな機能があります。

于 2013-01-03T16:59:04.000 に答える
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
于 2013-01-03T17:40:52.127 に答える