更新位置iPhone App

时间:2011-09-08 08:27:48

标签: iphone objective-c ios ios4 core-location

我正在尝试让应用在用户移动时更新用户的位置,并根据他的位置更新POI。它现在不起作用(即使用户移动很多,用户的位置也保持不变)。有人可以帮帮我吗?

- (void)viewDidLoad {

    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    locationManager.distanceFilter = 1000;
    [mapView removeAnnotations: mapView.annotations];
[locationManager startUpdatingLocation];


}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    homeCoordinate = newLocation.coordinate;
    if (!home1Annotation) {
        home1Annotation = [[MyMapAnnotation alloc] initWithCoordinate:homeCoordinate
                                                               title:@"Current Location"
                          ];
        [mapView addAnnotation:home1Annotation];
        [self adjustMapZoom];
        [self loadAndSortPOIs];
        for (int j =0 ; j <5 ; j++){
            [self displayPOIs];
        }
    }
}

我的地图注释

#import "MyMapAnnotation.h"


@implementation MyMapAnnotation
@synthesize coordinate;
#pragma mark initializers

// designated initializer
-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinateQ title:(NSString*) titleQ {
    if ( self = [super init]) {
        coordinate = coordinateQ;
        title = titleQ;

        [title retain];
    }
    return self;
}

-(void) dealloc {
    [title release];
    [super dealloc];
}

#pragma mark MKAnnotation implementation

-(NSString*) title { return title; }

-(NSString*) subtitle { return nil; }



@end

2 个答案:

答案 0 :(得分:0)

出现两件事:

1)要求最高准确度:kCLLocationAccuracyBest而不是kCLLocationAccuracyHundredMeters。不要设置distanceFilter或将其设置为零。

2)在didUpdateToLocation中添加一行

NSLog(@"Location update: %@",newLocation);

并观察日志文件输出。您的if语句使下面的代码块只运行一次。我希望有一个else块,如果home1Annotation已经存在,它将更新POI位置。类似的东西:

} else {
     [MyMapAnnotation setCoordinate:homeCoordinate];
}

(但我不知道你的班级MyMapAnnotation所以这是猜测)

答案 1 :(得分:0)

应该没有任何问题,一切都会有效,但是在你的情况下,当用户从最近检测到的距离为1公里(如此代码所指定的{{{{ 1}}