MKMapView 方法 'didUpdateUserLocation' 未调用

MKMapView mathod 'didUpdateUserLocation' not calling

我的文件中有这段代码,

@synthesize myLocation;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.locationManager = [[CLLocationManager alloc] init];
    myLocation.delegate = self;
    [self.locationManager requestWhenInUseAuthorization];
    [myLocation setShowsUserLocation: YES];

}

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

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

但是,方法 didUpdateUserLocation 没有调用。

我已经在应用程序中添加了所需的框架。

我在 info.plist 文件中添加了所有内容。但是它仍然没有调用。

这是我的 .h 文件,

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

@interface MapView : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *myLocation;
@property(nonatomic, retain) CLLocationManager *locationManager;
@end

兄弟我试过了你的code.The委托方法没有被调用

然后我发现了您没有在 viewDidLoad 方法末尾添加以下行的解决方案。请加

[self.locationManager startUpdatingLocation];

我在viewDidLoad方法中添加上行代码后,委托方法调用成功。

我刚刚解决了这个问题,

locationManager = [[CLLocationManager alloc] init];
    myLocation.delegate = self;
    [locationManager requestWhenInUseAuthorization];
    [locationManager startUpdatingLocation];
    [myLocation setShowsUserLocation: YES];

startUpdatingLocation

之后使用值为 YESsetShowUserLocation

您尚未调用更新位置的方法,因此未调用委托方法。您需要为 startUpdatingLocation 调用方法。

For Objective - C:

[locationManager startUpdatingLocation];

For Swift:

locationManager.startUpdatingLocation()