I can zoom the map out to include the user's location and the annotation I want; however I have 2 issues.
- I don't want the user's location to have a pin (see image1).
- The pins are too close to the edges, and sometimes under the nav/tab bars (see image2). Need some padding but not sure how to accomplish this.
Any help is much appreciated. Below is my code for the 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
UPDATE: Followed what Duncan said and got it to work. Here's my code:
// 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];