2013-08-19 4 views
0

Почему нельзя добавлять аннотации на mapView ??? Я получаю массив с аннотацией, а затем попытаться поставить их на карту (((карта пуста (это мой код и метод viewAnnotation где я нашел ошибку с моим собственным классом аннотаций (Почему нельзя добавлять аннотации на mapView?

@implimentation MyClass { 
    NSMutableArray *_annotation; 
} 
_agents = [[Database sharedDatabase] agentsList]; 
_mapView.delegate = self; 
[_agents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    [_annotations addObject:[[[BronAgentMapViewAnnotation alloc] initWithAgency:obj] autorelease]]; 
}]; 
NSLog(@"num of annot = %d", _annotations.count); //num of annot = 120 
_mapView.showsUserLocation = YES; 

if (_showUserLocation) { 
    _mapView.showsUserLocation = YES; 
} 
[_annotations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    [_mapView addAnnotation: obj]; 
}]; 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

if (self) { 
    // Custom initialization 
    _annotations = [[NSMutableArray array] retain]; 
} 

return self; 
} 



    - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    // in case it's the user location, we already have an annotation, so just return nil 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    // handle our three custom annotations 
    // 
    if ([annotation isKindOfClass:[BronAgentMapViewAnnotation class]]) 
    { 
     // try to dequeue an existing pin view first 
     static NSString *bronAgentMapViewAnnotationIdentifier = @"BronAgentMapViewAnnotationIdentifier"; 

     MKPinAnnotationView *pinView = (MKPinAnnotationView *) 
       [_mapView dequeueReusableAnnotationViewWithIdentifier:bronAgentMapViewAnnotationIdentifier]; 

     if (pinView == nil) 
     { 
      // if an existing pin view was not available, create one 
      MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] 
        initWithAnnotation:annotation reuseIdentifier:bronAgentMapViewAnnotationIdentifier] autorelease]; 

      customPinView.pinColor = MKPinAnnotationColorPurple; 
      customPinView.animatesDrop = NO; 
      customPinView.canShowCallout = YES; 

      // add a detail disclosure button to the callout which will open a new view controller page 
      // 
      // note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement: 
      // - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control; 
      // 
      UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      [rightButton addTarget:self 
          action:@selector(showDetails:) 
        forControlEvents:UIControlEventTouchUpInside]; 
      rightButton.tag = [(BronAgentMapViewAnnotation *) annotation annotationId]; 
      customPinView.hidden = [(BronAgentMapViewAnnotation *) annotation isHidden]; 
      customPinView.rightCalloutAccessoryView = rightButton; 

      return customPinView; 
     } 
     else 
     { 
      pinView.rightCalloutAccessoryView.tag = [(BronAgentMapViewAnnotation *) annotation annotationId]; 
      pinView.hidden = [(BronAgentMapViewAnnotation *) annotation isHidden]; 
      pinView.annotation = annotation; 
     } 
     return pinView; 
    } 
    return nil; 
} 

Могут помочь?

+0

http://www.appcoda.com/ios-programming-101-drop-a-pin-on-map-with-mapkit-api/ видеть эту ссылку .. – Jitendra

+0

вы уверены, что ваш массив не пуст после addobject? – james075

+0

Да, я уверен, я использую NSLog (@ "% @", _annotation.count), чтобы увидеть количество объектов ... – Nubaslon

ответ

0

Вы должны реализовать карту делегата. в annotationViews назначаются там.

http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
+0

У меня уже есть это в моем коде – Nubaslon

+0

проблема в том, что это не входит в эту функцию ((( – Nubaslon

+0

Кажется, вы не инициализировали mutablearray? –

0

Попробуйте создать свой собственный класс annotationView и называть viewforannotation так:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{ 
if ([annotation isKindOfClass:[yourAnnotationClass class]]) { 
    yourAnnotationView *annotationView = (yourAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annotation"]; 
    if (!annotationView) 
     annotationView = [[yourAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation" delegate:self]; 
    else 
     annotationView.annotation = annotation; 
    return annotationView; 
} 
     return [_mapView viewForAnnotation:annotation]; 
} 
0

Подключили Вы Ваше _mapView IBOutlet к MKMapView вы имеете в интерфейсе? Без этого он не знает, что MKMapView, который он рисует на экране, это тот, который вы вызываете _mapView и добавляете аннотации, поэтому он не будет просить рисовать эти аннотации.

0

Используйте это:

mapView=[[MKMapView alloc]initWithFrame:CGRectMake(0,0,320,460)]; 
    mapView.userInteractionEnabled=YES; 
    mapView.zoomEnabled=YES; 
    mapView.delegate=self; 
    [self.view addSubview:mapView]; 

    [self annotationPlacing]; 

    // 36.777313,-119.706405 36.120838,-115.33682 47.798397,-121.519775 47.606337,-122.330618 

} 

-(void)annotationPlacing 
{ 
    NSArray *arrLat=[[NSArray alloc]initWithObjects:@"36.777313",@"36.120838",@"47.798397",@"47.606337", nil]; 
    NSArray *arrLong=[[NSArray alloc]initWithObjects:@"-119.706405",@"-115.33682",@"-121.519775",@"-122.330618", nil]; 
    for(int i=0;i<4;i++) 
    { 
     CLLocationCoordinate2D location; 
     location.latitude=[[arrLat objectAtIndex:i]floatValue]; 
     location.longitude=[[arrLong objectAtIndex:i]floatValue]; 

     MKPointAnnotation *annottion=[[MKPointAnnotation alloc]init]; 
     annottion.coordinate=location; 
     [email protected]"India"; 
     [email protected]"Hyderabad"; 

     [arrAnnotation addObject:annottion]; 

     [mapView addAnnotation:annottion]; 

    } 
}