1

我正在尝试在地图上显示多个点。

我试过

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 为对象创建 Array ?

4

3 回答 3

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];
  }
于 2012-06-26T08:56:42.157 回答
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];

}
于 2012-06-26T08:55:48.460 回答
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];
    }
于 2012-06-26T09:08:02.283 回答