我可以缩小 map 以包含用户的位置和我想要的注释;但是我有两个问题。
非常感谢任何帮助。下面是我的 map View Controller 代码。
#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];
最佳答案
所以请考虑一下。
如果您不想显示用户的位置,请关闭 map 的显示当前位置标志。
打开位置管理器并请求位置更新。
在为您提供位置更新的位置管理器委托(delegate)方法中,获取用户的新位置,并将其与您要显示的注释进行平均 (lat1 + lat2)/2,(long1 + long2)/2。
这会给你一个介于 2 之间的中心点。然后取这两点之间的纬度增量,这两点之间的经度增量,将这些增量乘以一个小的比例因子,如 1.1 或 1.2,所以这两个点不在屏幕的边缘,并将这些数字输入 MKCoordinateSpanMake,并使用生成的跨度加上中心点来创建坐标区域,并将 map 设置到该坐标区域。
关于ios - 缩小以显示用户位置/注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23796042/