我可以缩小地图以包含用户的位置和我想要的注释;但我有两个问题。
非常感谢任何帮助。下面是我的地图视图控制器代码。
#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];
答案 0 :(得分:1)
所以要考虑一下。
如果您不想显示用户的位置,请将其关闭地图的显示当前位置标记。
开启位置管理器并询问位置更新。
在位置管理器委托方法中,为您提供位置更新,获取用户的新位置,并使用您要显示的注释(lat1 + lat2)/ 2,(long1 + long2)/ 2对其进行平均
那将给你一个中间点2.然后取这两个点之间的纬度增量,这两个点之间的经度增量,将这些增量乘以一个小的比例因子,如1.1或1.2,所以这两个点不在屏幕边缘,并将这些数字输入MKCoordinateSpanMake,并使用生成的跨度加上中心点来创建坐标区域,并将地图设置为该坐标区域。
答案 1 :(得分:0)
对于swift 3 :
let annotation = MKPointAnnotation();
annotation.coordinate = CLLocationCoordinate2DMake(LATITUDE, LONGITUDE);
self.mapView.addAnnotation(annotation);
let userlongitude = location.coordinate.longitude
let userlatitude = location.coordinate.latitude
let newDistance = CLLocation(latitude: userlatitude, longitude: userlongitude).distance(from: CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude))
let region = MKCoordinateRegionMakeWithDistance(location.coordinate, 2 * newDistance, 2 * newDistance)
let adjustRegion = self.mapView.regionThatFits(region)
self.mapView.setRegion(adjustRegion, animated:true)
在上面的代码" location"是CLLocation
,其中包含用户当前位置。