0

I'd like to have a map with just two points, the users location and a single annotation (it's for a pov). I have included what I have below. Ideally, I would like it to handle if the user is 100 feet away or 100 miles in displaying both the User and the annotation in the map view. I think it would be best if the location were at the center and the user were on the edge with some reasonable 20% buffer on the edge of the map view. I only need to support iOS 7 and greater.

Is there something like sizeToFit for annotations that takes into account users position as an annotation?

my MapViewController.h

@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
  @property (weak, nonatomic) IBOutlet MKMapView *mapView;
  @property (strong, nonatomic) CLLocationManager *locationManager;
@end

my MapViewController.m:

        #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

        #import "MapViewController.h"
        #import "MYUtility.h"



@interface MapViewController (){

  NSMutableArray *_yourAnnotationArray;
  MKPointAnnotation *_locationPoint;
  BOOL _firstTime;

}
@end

      @implementation MapViewController 

      - (void)viewDidLoad {

        [super viewDidLoad];
         _firstTime=YES;
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;

      if(IS_OS_8_OR_LATER) {
        //[self.locationManager requestWhenInUseAuthorization];
        //[self.locationManager requestAlwaysAuthorization];
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager startUpdatingLocation];
      }
      [self.locationManager startUpdatingLocation];
      [self.mapView setShowsUserLocation:YES];
      [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

      CGFloat latitude=MYLocationLatitude();
      CGFloat longitude=MYLocationLongitude();

      _locationPoint = [[MKPointAnnotation alloc] init];
      _locationPoint.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
      _locationPoint.title = @"Where am I?";
      _locationPoint.subtitle = @"I'm here!!!";
      [self.mapView addAnnotation:_locationPoint];
        }

        - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
        {
        }

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
      //if(_firstTime){
      NSLog(@"here is my thinking %@", [locations lastObject]);
      [self setMapStart];
    }

    -(void)setMapStart
    {
    //if(_firstTime){
      NSLog(@"at top of _firstTime being true");
      MKPointAnnotation *myPoint = [[MKPointAnnotation alloc] init];
      myPoint.coordinate = CLLocationCoordinate2DMake(34.035645, -118.233434);
      MKMapPoint annotationPoint = MKMapPointForCoordinate(self.mapView.userLocation.coordinate);
      //[_yourAnnotationArray addObject:annotationPoint];
      _yourAnnotationArray=[[NSMutableArray alloc] initWithObjects:_locationPoint,annotationPoint, nil];
      //NSLog(@"here is my count: %i",(unsigned long)[_yourAnnotationArray count]);
      [self.mapView showAnnotations:self.mapView.annotations animated:YES];  // <- determine when this has run
      _firstTime=NO;
    //}
    }
timpone
  • 19,235
  • 36
  • 121
  • 211
  • 1
    Check out `setVisibleMapRect:` (it's like the MKMapView equivalent of size to fit) and check out this answer for inspiration: http://stackoverflow.com/a/7141612/2274694 – Lyndsey Scott Dec 05 '14 at 03:59
  • thx Lyndsey, it looks like that Apple has added the showAnnotations method in iOS7 (actually the next answer in that question at http://stackoverflow.com/a/20161430/152825 ). Would you suggest using the previous method or the older method. MKMapKit is quite a pain but when it works is super nice. – timpone Dec 05 '14 at 04:04
  • 1
    I actually *love* Apple Maps and anything location related, but I'd forgotten about showAnnotations. I'd definitely recommend using that. And mapView.camera.altitude also sounds like it may be very useful in your case. – Lyndsey Scott Dec 05 '14 at 04:20
  • I love the end result but do iOS like 10% of time and just find that part really cryptic. Well, I'm really not getting the results with the code I have. Let me take a further look, the main problem seems to be that self.mapView.userLocation.coordinate isn't being read as a coordinate. In a small world phenomena, I actually emailed you through your web site about consulting... Hmm, working thtough this I think I need to be using `didUpdateLocations` to manage both of these annotations and it looks like it might be working. – timpone Dec 05 '14 at 04:38
  • ^^nah, that's not going to do it. – timpone Dec 05 '14 at 04:45
  • 1
    Well, you don't have to worry about one of the locations updating since it's fixed. And I'll check in with my assistant tomorrow about your message. – Lyndsey Scott Dec 05 '14 at 04:49
  • thx - yes, it will be something where we get the inforamtion first time correctly. thx for help tonight. Have to meet friends now - will update tomorrow, best and thx again for your help – timpone Dec 05 '14 at 05:05

0 Answers0