简体   繁体   中英

Show User's current location on map

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM