1

私のアプリでは、MKMapView を UITableView のビューとして追加しています。MapView の高さは 250 で、可視領域は 80 です。場所が見つかるたびに、その場所 (注釈) を可視領域 (MapView の下部 80 ピクセル) の中央に配置したいと考えています。

これは私がこれまでに持っているものです: https://dl.dropbox.com/u/3077127/MapViewDemo.mov

いろいろ試してみましたが、解決策が見つかりません。今のところテストしたものを削除しました。これは私のコードです:

float kMapHeaderHeight = 250.0f;
float kMapContentInset = 80.0f;
BOOL favFirstPositionFound = NO;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:NSLocalizedString(@"Favorites", @"FavoritesViewController")];

    // Set up the edit and add buttons.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, -kMapHeaderHeight, self.tableView.frame.size.width, kMapHeaderHeight)];
    [self.mapView setShowsUserLocation:YES];
    [self.mapView setDelegate:self];
    [self.mapView setUserInteractionEnabled:NO];

    [self.tableView addSubview:self.mapView];

    self.tableView.contentInset = UIEdgeInsetsMake(kMapContentInset, 0.0f, 0.0f, 0.0f);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.isDragging) {
        if (self.tableView.contentOffset.y < kMapContentInset-((self.view.bounds.size.height/3)*2))
        {
            NSLog(@"Far enough!");
        }
    }
}
[...]
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    location=newLocation.coordinate;

    MKCoordinateRegion region;
    region.center=location;

    MKCoordinateSpan span;

    if (!favFirstPositionFound)
    {
        span.latitudeDelta=.010;
        span.longitudeDelta=.010;
    }
    else {
        span.latitudeDelta = self.mapView.region.span.latitudeDelta;
        span.longitudeDelta = self.mapView.region.span.longitudeDelta;
    }

    region.span=span;

    favFirstPositionFound = YES;
    [self.mapView setRegion:region animated:YES];

    [self.locationManager stopUpdatingLocation];
}
4

1 に答える 1

1

わかりました、少し時間がかかりました...しかし、私は自分でそれを理解しました。

私が今していることは、現在の場所を使用して、CLLocationCoordinate2Dそこから対応するマップ ポイントを取得することです。次に、マップの可視四角形の幅と高さを使用して配置を調整します。

https://dl.dropbox.com/u/3077127/MapViewDemoResult.mov

解決策を見つけたばかりなので、コードはまだリファクタリングされていません...しかし、自由に自分で行ってください;)

最終結果:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    float offset = fabs(self.tableView.contentOffset.y);
    float diff = (1.0 - ((offset/2) / kMapHeaderHeight));

    MKMapRect r = [self.mapView visibleMapRect];
    MKMapPoint pt = MKMapPointForCoordinate(location);

    r.origin.x = pt.x - r.size.width * 0.50;
    r.origin.y = pt.y - r.size.height * diff;
    [self.mapView setVisibleMapRect:r animated:NO];

    MKCoordinateRegion region = self.mapView.region;

    MKCoordinateSpan span;
    span.latitudeDelta=.010;
    span.longitudeDelta=.010;

    region.span=span;

    [self.mapView setRegion:region animated:NO];

    if (scrollView.isDragging) {
        if (self.tableView.contentOffset.y < kMapContentInset-((self.view.bounds.size.height/3)*2))
        {
            NSLog(@"Halfway there!");
        }
    }
}
于 2012-12-08T17:59:38.160 に答える