1
votes

I need help with showing the user's location on my MapView. I have done everything I could possibly find online, but it still does not work.

I have a CLLocationManager in my AppDelegate that calls the locationUpdate in my viewController.

The (void)locationUpdate:(CLLocation *)location method gets called every time and the location coordinates is correct when logged with the NSLog(). Still, there is no "Blue Dot" on my iPhone's screen. The region is not set as well.

Please take a look and tell me if I am missing anything.

This is my header file:

//My .h file
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
#import <MapKit/MKReverseGeocoder.h>
@interface FirstViewController : UIViewController <MKMapViewDelegate>
{
    MKMapView *mapView;
}
-(void)locationUpdate:(CLLocation *)location;
-(void)locationError:(NSError *)error;
@end

This is my part of my implementation file:

//My .m file
#import "FirstViewController.h"
@implementation FirstViewController
- (void)viewDidLoad
{
    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.showsUserLocation = YES;
    mapView.mapType = MKMapTypeStandard;
    mapView.delegate = self;

    CLLocationCoordinate2D location;
    MKCoordinateRegion region;// = {{0.0,0.0},{0.0,0.0}};
    location.latitude = -33.8771;
    location.longitude = 18.6155;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.01;
    span.longitudeDelta = 0.01;
    region.span = span;
    region.center = location;
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
    [super viewDidLoad];
}
-(void)locationUpdate:(CLLocation *)location
{
    CLLocationCoordinate2D loc = [location coordinate];
    [mapView setCenterCoordinate:loc];
    if([mapView showsUserLocation] == NO)
    [mapView setShowsUserLocation:YES];
    NSLog(@"User Loc: %f, %f", mapView.userLocation.location.coordinate.latitude,
                     mapView.userLocation.location.coordinate.longitude);
    NSLog(@"In MapView: %@",[location description]);
}
@end

Thank you for your time! It is much appreciated!

1
Do you see any map at all? Why aren't you doing addSubView on the mapView? Are you running in the simulator?user467105
I am seeing the fully zoomed-out world-map. Then zoom in manually to my office. Looks like normal Maps on "Map"-view. The MapView is on one of the Panes on a TabBarController. I am running on both simulator and on my iPhone 4.LouwHopley
I think the map you see is one you created in Interface Builder but it's not connected to the one declared in your view controller.user467105
Possible Solution: in my HeaderFile, should "MKMapView *mapView;" not be "IBOutlet MKMapView *mapView;" and then I attach the mapView to the MKMapView in IB?LouwHopley
If you choose to use the one in IB, yes (and remove the alloc+init in viewDidLoad). Otherwise, delete it from IB and add [self.view addSubview:mapView]; after the alloc+init in viewDidLoad.user467105

1 Answers

3
votes

Your Map kit in Interface Builder needs to have the (Find User Location) checked!!