2014-01-27 15 views
0

我試圖製作一個使用設備當前位置的iOS7應用程序。我在我的Mac上使用iPhone模擬器,但我遇到了一些問題。每當我看到位置管理器出現時,即使在設置了自定義位置(從模擬器>調試>位置)後,它也會打印出0.000000的緯度和經度。Mac上的iOS 7模擬器無法使用自定義位置(也不要求權限)

此外,模擬器在打開應用程序時沒有要求使用當前位置的權限似乎很奇怪。有人知道這裏發生了什麼?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [super viewDidLoad]; 
    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
    [locationManager startUpdatingLocation]; 

    _location = [locationManager location]; 


    _coord.longitude = _location.coordinate.longitude; 
    _coord.latitude = _location.coordinate.latitude; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    _coord.longitude = _location.coordinate.longitude; 
    _coord.latitude = _location.coordinate.latitude; 
    printf("%f\n",self.coord.longitude); 
    printf("%f\n",self.coord.latitude); 
} 
+0

可能重複(http://stackoverflow.com/questions/21102917/is-it-possible-to-get-帶活動當前位置的模擬器) –

+0

默認情況下,模擬器位置設置爲無。因此,您需要進入模擬器菜單Debug - > Location並選擇一個蘋果的位置或選擇您自己的自定義座標。您也可以從Xcode底部的控制檯位置圖標中選擇一個。在那裏你可以選擇像倫敦或莫斯科等着名的ww位置。你也可以在方案中設置你的默認模擬器位置(編輯方案)。 –

+0

您在'viewDidLoad'方法中調用了兩次'[super viewDidLoad]'。 – Gavin

回答

0

您需要從委託方法didUpdateLocationToLocation獲取newLocation:fromLocation :.還實現didFailWithError委託方法。開始獲取更新位置需要一些時間,因此需要委託電話。

最後一個位置通常被緩存,因此檢查位置的時間戳並過濾舊的位置可能很明智。

編輯:

這是最清潔的例子我可以提供。在Xcode中啓動新項目,選擇Single View應用程序模板iPhone。不要觸摸故事板,只需用您的ViewController.m替換內容並在模擬器或設備中運行即可。如果在模擬器上,進入調試並設置一些位置,您將獲得控制檯中的座標。我也在視圖開啓或關閉時啓動和停止位置更新。

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

@interface ViewController() <CLLocationManagerDelegate> 

@property (strong, nonatomic) CLLocationManager *locationManager; 

@end 

@implementation ViewController 

#pragma mark - Location Manager delegate methods 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

    if ([newLocation.timestamp timeIntervalSinceNow] >= -300.0) { 

     NSLog(@"updated location with latitude %f longitude %f", newLocation.coordinate.longitude, newLocation.coordinate.latitude); 
    } 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [self.locationManager startUpdatingLocation]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 

    [self.locationManager stopUpdatingLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error  { 
    if(error.code == kCLErrorDenied) { 

     // alert user 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Access to location services is disabled" 
                 message:@"You can turn Location Services on in Settings -> Privacy -> Location Services" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alertView show]; 

    } else if(error.code == kCLErrorLocationUnknown) { 
     NSLog(@"Error: location unknown"); 
    } else { 
     NSLog(@"Error retrieving location"); 
    } 
} 

#pragma mark - Location Manager getter 

- (CLLocationManager *)locationManager 
{ 
    if (!_locationManager) { 
     _locationManager = [[CLLocationManager alloc] init]; 
     _locationManager.delegate = self; 
     _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
     _locationManager.distanceFilter = 60.0; 
    } 
    return _locationManager; 
} 

@end 
的[是否有可能與iPhone模擬器得到「活」的當前位置?]
+0

謝謝!當我回家的時候試試這個。 – Inbl

+0

當我嘗試覆蓋didUpdateToLocation時,它變得瘋狂,並說「方法的重複聲明」locationManager:didUpdateToLocation:fromLocation'「 – Inbl

+0

這意味着您的類中已經有方法didUpdateToLocation:fromLocation。您的原始問題是您嘗試以錯誤的方法訪問更新位置。 viewWillAppear會在所有內容加載之前調用一次,並且視圖將出現在屏幕上。但那時你的locationManager還沒有發佈任何位置更新。在調用didUpdateToLocation之前需要幾秒鐘的時間。要解決您的原始問題,只需將您的位置打印代碼移至didUpdateToLocation,如NSLog(@「%@」,newLocation);在//下面做你的東西。 –

相關問題