设置我的MapView;如何添加多个注释?

时间:2013-01-26 06:07:15

标签: annotations mkmapview mapkit

我在我的应用中创建了一个MapView,可以放大用户的当前位置,并且效果很好。请参阅下面的代码:

.h文件

@interface MapViewController : UIViewController  <MKMapViewDelegate> 

@property (nonatomic, strong) IBOutlet MKMapView *mapView;

@end

.m文件

- (void)viewDidLoad {

[super viewDidUnload];

    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];



    self.mapView.delegate = self;
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];


MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"You Are Here";
point.subtitle = @"Your current location";

[self.mapView addAnnotation:point];
}

此代码在用户的当前位置所在的位置放置注释。但是从这里开始,我想添加多个注释(以便用户可以看到它们周围的关键位置)。为了做到这一点,我需要添加/更改哪些代码?

感谢。

1 个答案:

答案 0 :(得分:0)

这可以通过许多不同的方式完成。这取决于您的数据来源和需求

这是添加两个

的方法
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"You Are Here";
point.subtitle = @"Your current location";

[self.mapView addAnnotation:point];


MKPointAnnotation *point2 = [[MKPointAnnotation alloc] init];
point2.coordinate = aDifferentCoordinate;
point2.title = @"You Are Here";
point2.subtitle = @"Your current location";

[self.mapView addAnnotation:point2];

这是一种在同一点添加一千个的方法

int i;
for(i = 0; i < 1000; i++)
{
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"You Are Here";
    point.subtitle = @"Your current location";

   [self.mapView addAnnotation:point];
}