跟踪当前位置的坐标

时间:2011-12-15 09:36:07

标签: iphone mkmapview

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

3 个答案:

答案 0 :(得分:1)

您过早访问该位置。根据设备的实际状态,可能还没有位置信息(因为NSLog是startUpdatingLocation之后的毫米或微小区域)。获取位置信息的正确方法是在delegate上设置locationManager,例如self并执行此操作:

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

这样,只要有可用的已知位置,您的应用就会收到通知。

答案 1 :(得分:0)

你需要实现这个委托函数......

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


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


}

并在.h

中实施CLLocationManagerDelegate

答案 2 :(得分:0)

在viewDidLoad方法的底部,您调用didUpdateToLocation。你不应该这样做。通过将地图的委托设置为此类,它将在更新位置时调用该方法。

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

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

应该是

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