2011-12-15 69 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