/ / IPhone SDK MapKIt Wiele punktów i adnotacji - iPhone, iOS 4, Mapkit

IPhone SDK MapKt Wiele punktów i adnotacji - iphone, ios4, mapkit

Jestem nowy w IPhone SDK Development Próbuję utworzyć aplikację z MapKit. Zrobiłem pierwszy bit, chcę dodać wiele pinów i adnotacji do aplikacji, ale zgubiłem się tutaj.

Poniżej znajduje się kod, w jaki sposób mogę dodać więcej pinów do tego kodu

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

Odpowiedzi:

2 dla odpowiedzi № 1

jesteś już na dobrej drodze, wystarczy ponownie użyć kodu, aby uzyskać wiele punktów. na przykład :

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

w rezultacie wyświetli 5 punktów o różnych współrzędnych. (o tej samej nazwie i podtytule).

Edytowane: pokaż inny obraz pinezki. musisz dodać nowe pole jako obraz NSString * do DisplayMap. i dodaj obraz ścieżki wewnątrz pętli.

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

}