在我的应用程序中,我添加了一个MKMapView作为UITableView的顶视图。 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];
}
答案 0 :(得分: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!");
}
}
}