0

Ok, here's my attempt at using CLLoactionManager

- (void)viewDidLoad {

    [super viewDidLoad];
    mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
    //mapView.showsUserLocation=TRUE;
    mapView.delegate=self;
    [self.view insertSubview:mapView atIndex:0];


    CLLocationManager *locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;

    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    mStoreLocationButton.hidden=FALSE;
    location=newLocation.coordinate;
    //One location is obtained.. just zoom to that location

    MKCoordinateRegion region;
    region.center=location;
    MKCoordinateSpan span;
    span.latitudeDelta=0.01;
    span.longitudeDelta=0.01;
    region.span=span;

    [mapView setRegion:region animated:TRUE];    
}

My problem is that [locationManager startUpdatingLocation]; doesn't seem to fire the next method. What am I missing? I've tried setting breakpoints in the second method, but they never catch. Obviously it's not being used.

Rob
  • 4,927
  • 12
  • 49
  • 54
Coltrane
  • 165
  • 1
  • 2
  • 12

2 Answers2

3

You should look into using a CLLocationManager to get the current location to feed your MKMapView with the correct coordinates.

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
MKMapView *map = [[MKMapView alloc] init];

[locationManager startUpdatingLocation];

CLLocationCoordinate2D _coordinate = locationManager.location.coordinate;
MKCoordinateRegion extentsRegion = MKCoordinateRegionMakeWithDistance(_coordinate, 800, 800); 

[map setRegion:extentsRegion animated:YES];
radesix
  • 5,834
  • 5
  • 24
  • 39
  • Do I need to create a new class to implement this? Or will this go in my above code? – Coltrane Jul 25 '12 at 03:46
  • This code is just the .m implementation. Obviously you would need the header files as well. I'll work up a more thorough example and post it within the next day or so. – radesix Jul 25 '12 at 20:12
0

This looks like a good place to start. In order to drop a pin, you need to know the coordinates. Using - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation will help you get the user's location so that you can create an MKAnnotation and MKPinAnnotationViews. Its pretty straight forward once you get started.

Community
  • 1
  • 1
RileyE
  • 10,874
  • 13
  • 63
  • 106
  • I tried to follow along with the guide, but `initWithCoordinate:coordinate andTitle:_title andSubtitle:_title` is giving me an error that there is no @interface that declares this selector. – Coltrane Jul 25 '12 at 04:14
  • Ahh, yes.. I forgot the annotation code.. thank you for pointing this out. I'll include this in my revised example to be posted soon. – radesix Jul 25 '12 at 21:19