0

为什么mapView不能加注解???我得到带有注释的数组,然后尝试将它们放到地图(((地图为空(这是我的代码和 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

您需要实现地图委托。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 回答
0

您是否已将 _mapView IBOutlet 连接到界面中的 MKMapView?没有它,它不知道它在屏幕上绘制的 MKMapView 是您正在调用 _mapView 并向其添加注释的那个,因此它不会要求绘制这些注释。

于 2013-08-19T20:17:01.600 回答
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

用这个:

    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 回答