MKMapView未显示注释点

时间:2013-05-19 00:51:48

标签: ios objective-c mkmapview

我试图显示我当前位置的位置和随机5点这样:

- (void)viewDidLoad {
    [super viewDidLoad];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
    otherlat = [NSNumber numberWithDouble:[currentLatitude doubleValue]];
    otherlong = [NSNumber numberWithDouble:[currentLongitude doubleValue]];

    [self.mapView setShowsUserLocation:YES];
    mapView.delegate = self;

    longi = [[NSMutableArray alloc] init];
    lati = [[NSMutableArray alloc] init];

    NSMutableArray *la = [[NSMutableArray alloc] init];
    NSMutableArray *lo = [[NSMutableArray alloc] init];
    la = [NSMutableArray arrayWithObjects:@"20", @"21", @"42", @"51", @"75", nil];
    lo = [NSMutableArray arrayWithObjects:@"60", @"21", @"82", @"181", @"35", nil];

    for (int x = 0; x < [la count]; x++) {
        otherlat = [NSNumber numberWithDouble:[[la objectAtIndex:x] doubleValue]];
        otherlong = [NSNumber numberWithDouble:[[lo objectAtIndex:x] doubleValue]];
        [longi addObject:otherlong];
        [lati addObject:otherlat];
    }

    myAnnotation *myAnnotation1 = [[myAnnotation alloc] init];
    for (int y = 0; y < [lati count]; y++) {  
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = [[lati objectAtIndex:y] doubleValue];
        theCoordinate.longitude = [[longi objectAtIndex:y] doubleValue];

        myAnnotation1.coordinate = theCoordinate;
        [mapView addAnnotation:myAnnotation1];
    }
}

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

    static NSString *myAnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myAnnotationIdentifier];
    customPinView.image = [UIImage imageNamed:@"purplepin.png"];
    customPinView.animatesDrop = YES;
    customPinView.canShowCallout = YES;

    return customPinView;
}

但是,mapview仅显示我当前的位置,而不显示其他5个位置。我不明白为什么它没有显示其他5个位置因为我

  [mapView addAnnotation:myAnnotation1];

1 个答案:

答案 0 :(得分:1)

您只需添加1个注释。你做了5次,但你总是覆盖以前的坐标。

//only 1 is allocated and then used/modified in every iteration of the loop!
myAnnotation* myAnnotation1=[[myAnnotation alloc] init];
for (int y =0; y < [lati count]; y++) {  
    ....

你需要创建不是1 myAnnotation1,但是...... 5个单独的:

//allocate & add in each iteration of the loop!
for (int y =0; y < [lati count]; y++) {  
    myAnnotation* myAnnotation1=[[myAnnotation alloc] init];
    ....