【问题标题】:MKMapView is not showing annotated pointsMKMapView 没有显示带注释的点
【发布时间】:2013-05-19 16:58:15
【问题描述】:

我正在尝试显示我当前位置的位置和随机 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;
}

但是,地图视图只显示我当前的位置,而没有显示其他 5 个位置。我不明白为什么它没有显示其他 5 个位置,因为我这样做了

  [mapView addAnnotation:myAnnotation1];

【问题讨论】:

  • 关键问题,将注解 alloc/init 移动到 for 循环内。您也不能拥有超过 180.0 的经度。您可以显着简化数组逻辑。你的viewForAnnotation 应该使用customPinView.pinColor = MKPinAnnotationColorPurple 而不是png。你的viewForAnnotation 真的应该在创建一个新的之前尝试dequeueReusableAnnotationViewWithIdentifier。如果您没有在注释等上设置 title 属性,则显示标注没有意义。
  • 感谢您的建议

标签: ios objective-c mkmapview


【解决方案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];
    ....

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    相关资源
    最近更新 更多