2011-12-15 74 views
0

我想跟蹤當前location..I使用TIS代碼來跟蹤它的緯度和經度座標...跟蹤當前位置的座標共同

-(void)ViewDidLoad{ 

     map = [[MKMapView alloc] initWithFrame:CGRectMake(0,46,320,370)]; 
      [map setDelegate:self]; 
      map.showsUserLocation=YES; 
      [map setZoomEnabled:YES]; 
      [self.view addSubview:map]; 
      [map setMapType:MKMapTypeStandard]; 
      [self locationManager:locationManager didUpdateToLocation:newlocation fromLocation:oldlocation];} 
- (void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation *)NewLocation 
      fromLocation:(CLLocation *)OldLocation 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
    [locationManager startUpdatingLocation]; 
    NSLog(@"%f %f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.latitude); 
} 

但我的應用程序崩潰, 我的日誌報告:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Map locationManager:didUpdateToLocation:fromLocation:]: unrecognized selector sent to instance 0x5863890' 

回答

1

您正在訪問的位置太早。根據設備的實際狀態,可能還沒有位置信息(因爲NSLog是startUpdatingLocation之後的毫秒或微秒)。獲取位置信息的正確方法是在您的locationManager上設置delegate,例如, self並實現這一點:

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

這樣,你的應用程序將盡快通知,因爲有可用的已知位置。

+0

感謝烏爾rply ...但伊茨撞毀我的應用程序.. – Icoder 2011-12-15 10:03:57

+0

任何崩潰信息/異常。 ..? – 2011-12-15 10:13:23

+0

終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因是:「 - [地圖的LocationManager:didUpdateToLocation:fromLocation:]:無法識別的選擇發送到實例0x5863890」 這是我崩潰報告 – Icoder 2011-12-15 10:23:26

0

你需要實現這個委託函數...

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation{ 
    userLocation=newLocation.coordinate; 


    NSLog(@"updated location is %f",newLocation.coordinate.longitude); 


} 

,同時也可在您的.H

0

CLLocationManagerDelegate在您的viewDidLoad方法您呼叫didUpdateToLocation的底部。你不應該那樣做。通過將地圖的代表設置爲此類,它將在位置更新時調用該方法。

您還需要在方法名

- (void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation *)NewLocation fromLocation:(CLLocation *)OldLocation 

增加空間應該是

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)NewLocation 
     fromLocation:(CLLocation *)OldLocation