【发布时间】:2014-05-22 00:38:44
【问题描述】:
我可以缩小地图以包含用户的位置和我想要的注释;但是我有 2 个问题。
非常感谢任何帮助。下面是我的地图视图控制器代码。
#import "BuildingsMapViewController.h"
#import "BuildingsAnnotations.h"
@interface BuildingsMapViewController ()
@end
@implementation BuildingsMapViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
BuildingsAnnotations *userAnnotation = [[BuildingsAnnotations alloc] init];
userAnnotation.coordinate = self.locationManager.location.coordinate;
self.mapView.delegate = self;
self.navigationItem.title = self.title;
CLLocationCoordinate2D startCenter = CLLocationCoordinate2DMake(42.334596, -83.049578);
CLLocationDistance regionWidth = 1500;
CLLocationDistance regionHeight = 1500;
MKCoordinateRegion startRegion = MKCoordinateRegionMakeWithDistance(startCenter, regionWidth, regionHeight);
[self.mapView setRegion:startRegion animated:YES];
self.coordinate = CLLocationCoordinate2DMake(self.geoPoint.latitude,self.geoPoint.longitude);
BuildingsAnnotations *annotation = [[BuildingsAnnotations alloc] init];
annotation.coordinate = self.coordinate;
annotation.title = self.navigationItem.title;
annotation.subtitle = @"Click for Directions";
[self.mapView addAnnotation:annotation];
[self.mapView setCenterCoordinate:annotation.coordinate animated:YES];
self.annotations = @[annotation, userAnnotation];
[self.mapView showAnnotations:self.annotations animated:YES];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
}
@end
更新:按照 Duncan 所说的进行操作。这是我的代码:
// Get user coordinates
self.userLat = self.locationManager.location.coordinate.latitude;
self.userLong = self.locationManager.location.coordinate.longitude;
// Initialize and set arrays
self.lats = [[NSMutableArray alloc] init];
self.lngs = [[NSMutableArray alloc] init];
[self.lats addObject:[NSNumber numberWithDouble:self.userLat]];
[self.lats addObject:[NSNumber numberWithDouble:annotation.coordinate.latitude]];
[self.lngs addObject:[NSNumber numberWithDouble:self.userLong]];
[self.lngs addObject:[NSNumber numberWithDouble:annotation.coordinate.longitude]];
// Sort numbers in arrays
[self.lats sortUsingSelector:@selector(compare:)];
[self.lngs sortUsingSelector:@selector(compare:)];
// Get smallest/biggest coordinates
double smallestLat = [self.lats[0] doubleValue];
double smallestLng = [self.lngs[0] doubleValue];
double biggestLat = [[self.lats lastObject] doubleValue];
double biggestLng = [[self.lngs lastObject] doubleValue];
// Get Center Point and Span
CLLocationCoordinate2D annotationsCenter = CLLocationCoordinate2DMake((biggestLat + smallestLat) / 2, (biggestLng + smallestLng) / 2);
MKCoordinateSpan annotationsSpan = MKCoordinateSpanMake((biggestLat - smallestLat) * 1.75, (biggestLng - smallestLng) * 1.75);
// Create and set Region
MKCoordinateRegion region = MKCoordinateRegionMake(annotationsCenter, annotationsSpan);
[self.mapView setRegion:region];
【问题讨论】:
-
“我不希望用户的位置有图钉”:那就不要将其添加为注释。 看起来您不需要 CLLocationManager首先是你在做什么。
-
由于 showAnnotations: 方法,我将其添加为注释(在某处看到有人推荐它)。与 CLLocationManager 相同。至少它显示了 2 个位置 =/.
-
当显示用户位置在地图视图中打开时,它会自动为您添加注释(蓝点)。但是,这可能不会在 viewDidLoad 中立即发生。即使使用位置管理器,它在您调用 startUpdatingLocation 后立即返回有效坐标的事实也是偶然的。当地图视图实际上有一个坐标时,它会调用 didUpdateUserLocation。 showAnnotations 或 setRegion 应该在 didUpdateUserLocation 中完成(带有一些标志只做一次)。
-
对于填充区域,我推荐this approach,但调用
setVisibleMapRect:edgePadding:animated:而不仅仅是setVisibleMapRect:animated:。 -
几件事。如果您在用户位置上获得了一个图钉,则需要更改 viewForAnnotation: 方法以检查用户位置,如果当前注释是用户位置,则返回 nil:
if(annotation == mapView.userLocation) return nil。
标签: ios objective-c mapkit core-location