无法确定和放大当前用户位置

current user location not being able to be determined and zoomed in on

我正在尝试获取地图视图上的当前用户位置并将其放大。 这是我的代码-

 #import "WhereamiAppDelegate.h"
 #import "WhereamiViewController.h"


  @implementation WhereamiAppDelegate

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[WhereamiViewController alloc] initWithNibName:@"WhereamiViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

@interface WhereamiViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate,UITextFieldDelegate>
 {

//@public RootObject *rootObj;
  CLLocationManager *locationManager;

 IBOutlet MKMapView *worldView;
 IBOutlet UIActivityIndicatorView *activityIndicator;
 IBOutlet UITextField *locationTitleField;
 }
-(IBAction)buttonDidGetPressed:(id)sender;
-(BOOL)textFieldShouldReturn:(UITextField *)textField;
-(void)findLocation;
-(void)foundLocation:(CLLocation *)loc;
@end

@implementation WhereamiViewController
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
   NSLog(@"%@", NSStringFromSelector(_cmd));
   if (self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    // rootObj= [[RootObject alloc] init];//RootObject initialised
    // NSLog(@"RootObject–– %@",rootObj);
     locationManager= [[CLLocationManager alloc] init];

     [locationManager setDelegate:self];//self is Whereamicontroller. The delegate pointer is of type id<CLLocationManagerDelegate> and is an ivar of CLLocationManager.
     [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

 }
 return self;
}

-(void) viewDidLoad{
  NSLog(@"%@",NSStringFromSelector(_cmd));
  worldView.showsUserLocation=YES;
}

-(void)mapViewWillStartLocatingUser:(MKMapView *)mapView{
    NSLog(@"%@", NSStringFromSelector(_cmd));
 }
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
   NSLog(@"%@",NSStringFromSelector(_cmd));

   CLLocationCoordinate2D centerCoordinate= [userLocation coordinate]; //get the coordinate of current location.
   NSLog(@"%@ (%f, %f)",userLocation.location,centerCoordinate.latitude,centerCoordinate.longitude);
   MKCoordinateSpan span= MKCoordinateSpanMake(250, 250);//Structure members
   MKCoordinateRegion mapPortionToDisplay= MKCoordinateRegionMakeWithDistance(centerCoordinate, span.latitudeDelta, span.longitudeDelta);//span.latitudeDelta=250 and span.longitudeDelta=250

   [worldView setRegion:mapPortionToDisplay animated:YES];
//        [worldView setRegion:mapPortionToDisplay];

   [worldView regionThatFits:mapPortionToDisplay];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ //CLLocationManagerDelegate method implementation
  NSLog(@"%@", NSStringFromSelector(_cmd));
//    NSTimeInterval t0=[[locations lastObject] timeIntervalSinceNow];
NSLog(@"current location–– %@",(CLLocation *)[locations lastObject]);  
 }

输出结果如下-

2017-08-25 22:16:19.178 Whereami2[1601:758525] initWithNibName:bundle:

2017-08-25 22:16:19.294 Whereami2[1601:758525] viewDidLoad

2017-08-25 22:16:20.607 Whereami2[1601:758525] mapViewWillStartLocatingUser:

从上面几行,可以清楚地看到 mapView:didUpdateUserLocation: 消息根本没有传递给地图视图的委托。 locationManager:didUpdateLocations: 也是如此。 showsUserLocations 属性 应该发送 CLLocationManager 的委托,一个 viewcontroller(与 MKMapView 的委托相同),此消息也在其实现中。不是吗?

您已将 locationManager 声明为实例变量,但未合成它的 getter 和 setter。它可能正在被释放。只需将其设为 属性:

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

至于 worldView,我看不出你在哪里设置了 worldView.delegate = self