2017-08-25 61 views
-1

我試圖在地圖視圖中獲取當前用戶位置並放大它。 這是我的代碼 -當前用戶位置無法確定和放大

#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年8月25日22:16:19.178 Whereami2 [1601:758525] initWithNibName:束:

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

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

從以上幾行看,它清楚地看到mapView:didUpdateUserLocation:消息沒有傳遞給映射視圖的委託。 locationManager:didUpdateLocations:也是如此。 showsUserLocations屬性應該發送CLLocationManager的委託,一個viewcontroller(與MKMapView的委託相同),這個消息也在其實現中。不是嗎?

+0

你能證明你的LocationManager聲明?和你是如何初始化你的視圖控制器? –

+0

@interface WhereamiViewController:UIViewController { CLLocationManager * locationManager; IBOutlet MKMapView * worldView; IBOutlet UIActivityIndi​​catorView * activityIndi​​cator; IBOutlet UITextField * locationTitleField; }這是我的viewcontroller,它符合這個問題的兩個協議 – rahulbsb

+0

在你的viewDidLoad NSLog(@「%@」,locationManager)中加入這一行並讓我知道打印什麼 –

回答

1

您已聲明locationManager作爲實例變量,而不合成它的getter和setter。它可能正在被釋放。只是讓一個屬性:

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

@property (nonatomic, strong) CLLocationManager *locationManager; 

@end 

至於worldView,我沒有看到你設置類似worldView.delegate = self

+0

我之前在XIB文件中通過控制 - 從MKMapView將連接拖到文件的所有者(Whereamiviewcontroller)並將它的委託ivar設置爲指向相同的,然後再執行此步驟。 – rahulbsb

+0

我也嘗試在測試設備上運行。沒有不同。我該怎麼做? – rahulbsb

+0

堅持一秒..我只是NSLogged worldView。在一個視圖控制器方法中委派。原來是NULL。但是這怎麼可能?它已經在XIB文件中將MKMapView對象的委託指針指向了Whereamiviewcontroller。 – rahulbsb