2012-12-07 117 views
1

在我的應用程序中,我添加了一個MKMapView作爲UITableView的ontop視圖。 MapView的高度爲250,可見區域爲80. 每當找到某個位置時,我都希望將該位置(註釋)居中在MapView的底部80px的可見區域中。將中心映射到可見區域

這是我到目前爲止有: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]; 
} 

回答

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!"); 
     } 
    } 
} 
+0

找到一個我需要修復的bug,就是如果你向上拉桌子,那麼節頭將保持其位置,但是內容將向上滾動到地圖視圖中。 –