IPhone SDK MapKIt多点和注释

时间:2011-01-27 16:30:09

标签: iphone ios4 mapkit

我是IPhone SDK开发的新手我正在尝试使用MapKit创建应用程序我已经完成了第一项我想在应用程序中添加多个引脚和注释但我在这里迷失了。

以下是代码如何为此代码添加更多引脚

-(void)viewDidLoad{
 [super viewDidLoad];

 [mapView setMapType:MKMapTypeStandard];
 [mapView setZoomEnabled:YES];
 [mapView setScrollEnabled:YES];
    MKCoordinateRegion region={{0.0,0.0,},{0.0,0.0}};
 region.center.latitude = 26.438047;
 region.center.longitude = 50.116422;
 region.span.latitudeDelta=0.01f;
 region.span.longitudeDelta=0.01f;
 [mapView setRegion:region animated:YES]; 
 [mapView setDelegate:self]; 

 DisplayMap *ann = [[DisplayMap alloc] init];
 ann.title = @"Corporate Office";
 ann.subtitle =@"King Khalid Street";
 ann.coordinate=region.center;
 [mapView addAnnotation:ann];
 }

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>) annotation{
 MKPinAnnotationView *pinView=nil;
 if (annotation != mapView.userLocation) {
  static NSString *defaultPinID = @"com.invasivecode.pin";
  pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
  if (pinView ==nil) pinView = [[[MKPinAnnotationView alloc]
            initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
  pinView.pinColor=MKPinAnnotationColorRed;
  pinView.canShowCallout=YES;
  pinView.animatesDrop=YES;
  pinView.calloutOffset= CGPointMake(-5, 5);
  }
 else {
  [mapView.userLocation setTitle:"I am here"];
 }
 return pinView;
}

1 个答案:

答案 0 :(得分:2)

您已经走上正轨,只需重复使用您的代码即可获得多个积分。 例如:

 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];

结果,将在不同的坐标中显示5个点。 (同名和副标题)。

编辑:显示不同的图像。你必须将新字段作为NSString *图像添加到DisplayMap。并在里面添加你的路径图像for循环。

- (MKAnnotationView *) mapView:(MKMapView *)amapView viewForAnnotation:(id      <MKAnnotation>) annotation
    {
 NSLog(@"pinnview before release %d",[pinView retainCount]);

if (pinView !=nil) {
    pinView =nil;
    [pinView release];
}
NSLog(@"pinnview after release %d",[pinView retainCount]);

// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

if(annotation != map.userLocation)
{

    static NSString *defaultPinID = @"your-pin";

    pinView = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

    if ( counting < [map.annotations count])
    {
        counting++;

        pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];


        for(DisplayMap* a in map.annotations)
        {
            if (annotation == a){
                pinView.image =
                [UIImage imageWithContentsOfFile:
                 [[NSBundle mainBundle] pathForResource:a.image ofType:nil]];   
            }
        }
        pinView.centerOffset= CGPointMake(0,-10);
        pinView.canShowCallout = YES;


    }

}

return pinView;

}