Iphone 跟踪当前位置的坐标

Iphone 跟踪当前位置的坐标,iphone,mkmapview,Iphone,Mkmapview,我想跟踪当前位置的纬度和经度坐标..我使用tis代码跟踪它 -(void)ViewDidLoad{ map = [[MKMapView alloc] initWithFrame:CGRectMake(0,46,320,370)]; [map setDelegate:self]; map.showsUserLocation=YES; [map setZoomEnabled:YES]; [s

我想跟踪当前位置的纬度和经度坐标..我使用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'

你访问该位置太早了。根据设备的实际状态,可能还没有位置信息(因为您在
startUpdatingLocation
之后的NSLog是毫秒或微秒)。获取位置信息的正确方法是在您的
locationManager
上设置一个
代表,例如
self
,并执行以下操作:

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

这样,一旦已知位置可用,您的应用程序就会收到通知。

您需要实现此委派功能

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


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


}

并在调用didUpdateLocation的viewDidLoad方法底部的.h

中实现
CLLocationManagerDelegate
。你不应该那样做。通过将映射的委托设置为此类,它将在更新位置时调用该方法

您还需要在方法名称中添加空格

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

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

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Map locationManager:didUpdateToLocation:fromLocation:::无法识别的选择器发送到实例0x5863890”这是我的崩溃报告您是否在类
Map
中正确实现了
didUpdateToLocation
方法(无键入)?由于您似乎已将
Map
对象作为委托传递,因此需要在那里实现该方法。检查它是否存在,以及里面是否没有输入错误。很好,它仍然缺少委托方法的实现,并且它是否在正确的类中(即,委托方法是否与上面使用的
self
作为委托时在同一个类中
viewDidLoad
),我仍然得到同样的问题,但是你仍然自己调用这个函数,这是你不应该做的。当CoreLocation有一个位置可以提供给您时,让它这样做。感谢您的rply。。。。但我也试过了…我打电话给tat代表。。。。。我不知道是不是?我也用了这个委托-(void)mapView:(MKMapView*)mapView didUpdateUserLocation:(MKUserLocation*)userLocation bt仍然存在相同的问题