0

mapView に注釈を追加できないのはなぜですか? 注釈付きの配列を取得し、それらを map((( マップは空です( これは私のコードと viewAnnotation メソッドで、自分の注釈クラスで間違いを見つけました(

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

助けられる?

4

4 に答える 4

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;
        annottion.title=@"India";
        annottion.subtitle=@"Hyderabad";

        [arrAnnotation addObject:annottion];

        [mapView addAnnotation:annottion];

    }
}
于 2013-10-07T14:21:31.470 に答える
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];
}
于 2013-08-19T12:54:48.577 に答える
0

インターフェイスにある MKMapView に _mapView IBOutlet を接続しましたか? それがなければ、画面に描画している MKMapView が _mapView を呼び出して注釈を追加しているものであることがわからないため、それらの注釈を描画するように求められません。

于 2013-08-19T20:17:01.600 に答える
0

マップ デリゲートを実装する必要があります。そこにannotationViewsが割り当てられます。

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

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
于 2013-08-19T11:58:55.863 に答える