我想在地图上显示多个点。
我试过
DisplayMap *ann = [[DisplayMap alloc] init];
for( int i =1;i<=5;i++ ){
region.center.latitude = 26.438047+i;
region.center.longitude = 50.116422+i;
ann.title = [NSString stringWithFormat:@"title %d",i)];
ann.subtitle =[NSString stringWithFormat:@"subtitle %d",i)];
ann.image = [NSString stringWithFormat@"image_%d.png",i];
ann.coordinate=region.center;
[mapView addAnnotation:ann];
}
[ann release];
此代码,但只有最后一个引脚在地图上。
我可以通过listArray解决这个问题吗?如果可以,我如何为具有objective-c的对象创建数组?
答案 0 :(得分:3)
您想为每个注释创建DisplayMap对象 修改您的代码如下
for( int i =1;i<=5;i++ ){
DisplayMap *ann = [[DisplayMap alloc] init];
region.center.latitude = 26.438047+i;
region.center.longitude = 50.116422+i;
ann.title = [NSString stringWithFormat:@"title %d",i)];
ann.subtitle =[NSString stringWithFormat:@"subtitle %d",i)];
ann.image = [NSString stringWithFormat@"image_%d.png",i];
ann.coordinate=region.center;
[mapView addAnnotation:ann];
} 这将有效
或者你可以使用数组
NSMutableArray *annotationArray = [NSMutableArray alloc]init];
for(int i = 0; i <= 5; i++) {
DisplayMap *ann = [[DisplayMap alloc] init];
region.center.latitude = 26.438047+i;
region.center.longitude = 50.116422+i;
ann.title = [NSString stringWithFormat:@"title %d",i)];
ann.subtitle =[NSString stringWithFormat:@"subtitle %d",i)];
ann.image = [NSString stringWithFormat@"image_%d.png",i];
ann.coordinate=region.center;
[annotationArray addObject:ann];
}
for(DisplayMap *ann in annotationArray) {
[mapView addAnnotation:ann];
}
答案 1 :(得分:1)
你应该在for循环中分配和释放ann。
for( int i =1;i<=5;i++ ){
region.center.latitude = 26.438047+i;
region.center.longitude = 50.116422+i;
ann = [[Annotation alloc]init]; //Replace according to your code
ann.title = [NSString stringWithFormat:@"title %d",i)];
ann.subtitle =[NSString stringWithFormat:@"subtitle %d",i)];
ann.image = [NSString stringWithFormat@"image_%d.png",i];
ann.coordinate=region.center;
[mapView addAnnotation:ann];
[ann release];
}
答案 2 :(得分:1)
试试这个
for (int i=0; i<5; i++) {
lat=m_strLatittude+i;
lon=m_strLongitude+i;
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [lat doubleValue];
region.center.longitude = [lon doubleValue] ;
region.span.latitudeDelta=mapView.region.span.latitudeDelta;
region.span.longitudeDelta=mapView.region.span.longitudeDelta;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc]init];
ann.title = name1;
ann.subtitle = Address2;
ann.coordinate = region.center;
[mapView addAnnotation:ann];
[ann release];
}