2014-10-31 45 views
1

我正嘗試創建一個應用程序,該應用程序使用MKMapView打開一個地圖,然後通過創建一個委託給CLLocationManger查找用戶的當前位置。在iOS模擬器上使用CLLocationManager來查找經度和緯度

我已在下面發佈我的代碼。我現在遇到的問題是,雖然地圖確實出現在打開這個模擬器時,它並沒有給出NSLog中應該傳遞經緯度的位置或新標題。

我是Objective-C和App Development的新手,有沒有人看到我是否缺少CLLocationManger的一些協議?感謝你的幫助。

對不起,代碼有點馬虎,很快粘貼它。

#import "ViewController.h" 
    #import <MapKit/MapKit.h> 
    #import <CoreLocation/CoreLocation.h> 

    @interface ViewController() <CLLocationManagerDelegate> 
    @property (nonatomic,strong)CLLocationManager * myLocationManger; 
    @property (nonatomic,strong) IBOutlet MKMapView *myMapView; 
    @end 

    @implementation ViewController 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 
     NSLog(@"insdie view did load"); 

    // Setting the map to be big as the view 
     self.myMapView =[[MKMapView alloc]init]; 

//Initial Property to Map 
    self.myMapView =[[MKMapView alloc] init]; 

//Set type to Standard 
self.myMapView.mapType = MKMapTypeStandard; 

//Set Mask for AutoReszing 
self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth |            UIViewAutoresizingFlexibleHeight; 

//Add the View! 
[self.view addSubview:self.myMapView]; 

///*Now lets Use my CLLocation Manager to Locate the iPhone's Posistion*//// 



//Chekcs if Location Services are Enabled 
if ([CLLocationManager locationServicesEnabled]) { 
    self.myLocationManger = [[CLLocationManager alloc] init]; 
    self.myLocationManger.delegate = self; 

    [self.myLocationManger startUpdatingLocation]; 



} 
else{ 
    //Location Services are available we will need software to ask to turn this On 
    //The user is SOL if they refuse to turn on Location Services 
    NSLog(@"Location Services not enabled"); 


} 



} 

    -(void)locationManager:(CLLocationManager *)manager 
     didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation 
    { 
     //This method will show us that we recieved the new location 
     NSLog(@"Latitude = %f",newLocation.coordinate.latitude); 
     NSLog(@"Longitude =%f",newLocation.coordinate.longitude); 

    } 

    -(void)locationManager:(CLLocationManager *)manager 
    didFinishDeferredUpdatesWithError:(NSError *)error{ 
     NSLog(@"Error with Updating"); 
    } 



    -(void)locationManager:(CLLocationManager *)manager 
    didFailWithError:(NSError *)error 
    { 
//Failed to recieve user's location 
NSLog(@"failed to recived user's locatio"); 
    } 
+0

請仔細閱讀「位置和地圖編程指南」。有一個條目必須添加到Info.plist中。 – rmaddy 2014-10-31 02:01:44

回答

相關問題