我一直在搞乱 mapkit,我有一个 MKMapView,我将它设置为显示使用位置。我想要做的是将地图集中在用户位置上,而不是不断地,而是一次(加载地图视图时)。我认为这是一个相当典型的用例,但我无法找到一个简单简洁的解决方案。
问问题
232 次
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 回答