2010-09-28 77 views
1

我需要在用戶當前位置的某個範圍內,將隨機註釋的'xx'添加到MKMapView。但我無法弄清楚如何去做。向MKMapView添加隨機註釋

你們能幫助我嗎?

+0

正是你無法從該怎麼辦? – Vladimir 2010-09-28 12:33:19

+0

找出如何將隨機註釋添加到MKMapView – Kleemann 2010-09-28 13:06:12

回答

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 
+0

RAND_MAX值? – 2012-11-17 02:05:00