xcode MKMapView didUpdateUserLocation 没有被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7646176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:04:10  来源:igfitidea点击:

MKMapView didUpdateUserLocation not being called

objective-cxcodemkmapview

提问by user973985

I am following the big nerd ranch guide book and have modified my app delegate.h to look like this:

我正在关注大书呆子牧场指南,并将我的应用程序 delegate.h 修改为如下所示:

   #import <UIKit/UIKit.h>
   #import <CoreLocation/CoreLocation.h>
   #import <MapKit/MapKit.h>
   @interface WhereamiAppDelegate : NSObject <UIApplicationDelegate,CLLocationManagerDelegate, MKMapViewDelegate>
   {   
       IBOutlet UITextField *locationTitleField;
       IBOutlet UIActivityIndicatorView *activityIndicator;
       IBOutlet MKMapView *worldView;
       CLLocationManager *locationManager;
   }
  @property (nonatomic, retain) IBOutlet  UIWindow *window;
  @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
  @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
  @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  - (void)saveContext;
  - (NSURL *)applicationDocumentsDirectory;
 @end

The .m looks like this:

.m 看起来像这样:

 #import "WhereamiAppDelegate.h"
 @implementation WhereamiAppDelegate
 @synthesize window = _window;
 @synthesize managedObjectContext = __managedObjectContext;
 @synthesize managedObjectModel = __managedObjectModel;
 @synthesize persistentStoreCoordinator = __persistentStoreCoordinator;

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

     CLLocationCoordinate2D loc = [userLocation coordinate];
     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
     [worldView setRegion:region animated:YES];
     NSLog(@"didUpdateUserLocation is called");
  }
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {
     // Override point for customization after application launch.

// Create location manager object
     locationManager = [[CLLocationManager alloc] init];
// There will be a warning from this line of code; ignore it for now
     [locationManager setDelegate:self];
// We want all results from the location manager
     [locationManager setDistanceFilter:kCLDistanceFilterNone];
// And we want it to be as accurate as possible
// regardless of how much time/power it takes
     [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// Tell our manager to start looking for its location immediately
// [locationManager startUpdatingLocation];
     [worldView setShowsUserLocation:YES];
// This line may say self.window, don't worry about that
     [self.window makeKeyAndVisible];
     return YES;
 }
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
 {
      NSLog(@"%@", newLocation);
 }
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
      NSLog(@"Could not find location: %@", error);
 }

When I open the app it should ZOOM into my location. But it doesn't zoom, so i put an NSLog in the didUpdateUserLocation to see if it was called. But it was never printed, so it wasn't called. How do I fix this?

当我打开应用程序时,它应该放大到我的位置。但它不会缩放,所以我在 didUpdateUserLocation 中放了一个 NSLog 来查看它是否被调用。但它从未打印过,所以它没有被调用。我该如何解决?

回答by Edgar

Same thing happened to me...

同样的事情发生在我身上...

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    [worldView setDelegate:self];
    ...
}

Thks

谢谢