1

I am new to IPhone SDK Development i am trying to make an application with MapKit i have done the first bit i want to add multiple pins and annotation to the application but i am lost here.

Following is the code how can i add more pins to this code

-(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;
}
3
  • Same way you are adding the first pin. What's the problem? By the way, you should do [ann release]; after the addAnnotation. Commented Jan 27, 2011 at 16:49
  • Hi i tried several thing [ann release]; and redo the code with different variables and tried for loop didnt worked help please? Commented Jan 28, 2011 at 1:07
  • Show the for-loop you tried (add it to the question). Commented Jan 28, 2011 at 2:29

1 Answer 1

2

you are on the right track already, just reuse your code to make multiple points. for example :

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

in result, will display 5 points in different coordinate. (with same name and subtitle).

Edited: show different pin image. you have to add new field as NSString *image to DisplayMap. and add your path image inside for loop.

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

}

Sign up to request clarification or add additional context in comments.

8 Comments

Hi thanks for the input i tried this way but it stops showing the pin i tried this approach updating my question
for(int i=1; i<5;i++){[mapView setMapType:MKMapTypeStandard]; [mapView setZoomEnabled:YES]; [mapView setScrollEnabled:YES]; MKCoordinateRegion region={{0.0,0.0,},{0.0,0.0}}; region.center.latitude = 26.438047+i; region.center.longitude = 50.116422+i; 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]; } [release ann];}
@tlikyu: You have to create a new instance for each annotation to create multiple pins (like user592514 is doing in his example).
@user592514: The code in your comment is ok overall but you probably don't see all the pins because the region span is set to 0.01 but your pins are being placed across 5.0 degrees (so increase the span to see them). Also, move all the mapview settings to outside the loop (it's not good or necessary to change the map's region for each pin added).
ok thank you so much so if i want to you string array fro lattitude and longitude i mae an array use count and then refer the longitude and latitude and i have to increase the span right ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.