-1

I am trying to display user's current location on the map but for some reason, does not takes my default coordinates.

I have a MKMapView class that called inside a UIViewController.

MapView.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface MapView : MKMapView <MKMapViewDelegate, CLLocationManagerDelegate>{

    CLLocationManager *locationManager;
}

@end

MapView.m

#import "MapView.h"

@interface MapView ()

@end

@implementation MapView

- (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {

        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;

        self.delegate = self;

        [self addAnnotation:[self userLocation]];
        [locationManager startUpdatingLocation];
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation");

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250, 250);
    [self setRegion:[self regionThatFits:region] animated:YES];

    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"Where am I?";
    point.subtitle = @"I'm here!!!";

    [self addAnnotation:point];
}

- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    NSLog(@"viewForAnnotation");
    static NSString *identifier = @"MyAnnotation";

    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        NSLog(@"Is the user %f, %f", [annotation coordinate].latitude, [annotation coordinate].longitude);
        return nil;
    }

    return nil;
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    NSLog(@"didAddAnnotationViews");
    for (MKAnnotationView *view in views){
        if ([[view annotation] isKindOfClass:[MKUserLocation class]]){

            MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([[view annotation] coordinate] , 250, 250);

            [mapView setRegion:region animated:YES];
        }
    }
}

@end

I am getting the output below once access the map.

viewForAnnotation
Is the user 0.000000, 0.000000
didAddAnnotationViews

I can't understand why my didUpdateUserLocation: never get called even if re-submit custom coordinates.

What I am missing here?

OutOfBoundsException
  • 756
  • 1
  • 11
  • 26

2 Answers2

0

You're using a CLLocationManager and setting it's delegate, but your delegate methods are all methods of MKMapViewDelegate.

You can either set the mapview's delegate, or use the locationManager:didUpdateLocations: CLLocationManagerDelegate method.

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • Oh, how I missed that! You are right. I was testing some solutions before and that's why I have CLLocationManager delegate. I have replace the `[self addAnnotation:[self userLocation]];` with `[self setShowsUserLocation:YES];` and seems that is working correctly now. – OutOfBoundsException Jul 07 '13 at 12:06
  • 1
    you don't need a `CLLocationManager` when you use the method `setShowsUserLocation` – Felix Jul 07 '13 at 12:34
0

If it is important to you that the user have the option to turn his location on and off on the map then Nevan King is correct. However, if you are OK with continually showing the user location then no scripting is necessary. Simply click on your mapView in the Storyboard and on the right (in attributes, I think) check the "shows user location" box.

Max von Hippel
  • 2,856
  • 3
  • 29
  • 46