将随机注释添加到MKMapView

时间:2010-09-28 12:23:15

标签: iphone annotations mkmapview

我需要在用户当前位置的特定范围内向MKMapView添加“x”量的随机注释。但我无法弄清楚如何去做。

你们有些人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

这样的事情可能有所帮助:

for(int i=0;i<10;i++) {
    CGFloat latDelta = rand()*0.125/RAND_MAX - 0.02;
    CGFloat lonDelta = rand()*0.130/RAND_MAX - 0.08;
    CLLocationCoordinate2D newCoord = {myLocation.coordinate.latitude+latDelta, myLocation.coordinate.longitude+lonDelta};
    DisplayMap *ann = [[DisplayMap alloc] init];
    ann.title = @"Some Pin";
    ann.subtitle = [NSString stringWithFormat:@"Pin %i subtitle",i+1];
    ann.coordinate = newCoord;
    [mapView addAnnotation:ann];
    [ann release]; 
}

其中: myLocation是 CLLocation 实例。 DisplayMap 是:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface DisplayMap : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;

}

@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;

@end