在我的项目中,我将这些信息填充到地图中

for  (int i = 0; i<arrayResults.count; i++){

        object = [arrayResults objectAtIndex:i];

        if ([[object iden]intValue] == 1) {

            type = @"type_one";

            CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:@"latitude"] floatValue], [[[object coord]objectForKey:@"longitude"] floatValue]);

            MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
            annotationPoint.coordinate = annotationCoord;
            annotationPoint.title = [NSString stringWithFormat:@"%@",[object nome_obj]];
            annotationPoint.subtitle = [NSString stringWithFormat:@"%@",[object address]];
            [map addAnnotation:annotationPoint];
        }

        else if ([[object iden]intValue] == 2) {

          type = @"type_two";

            CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:@"latitude"] floatValue], [[[object coord]objectForKey:@"longitude"] floatValue]);

            MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
            annotationPoint.coordinate = annotationCoord;
            annotationPoint.title = [NSString stringWithFormat:@"%@",[object nome_obj]];
            annotationPoint.subtitle = [NSString stringWithFormat:@"%@",[object address]];
            [map addAnnotation:annotationPoint];
        }

        else if ([[object iden]intValue] == 3) {

            type = @"type_three";

            CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:@"latitude"] floatValue], [[[object coord]objectForKey:@"longitude"] floatValue]);

            MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
            annotationPoint.coordinate = annotationCoord;
            annotationPoint.title = [NSString stringWithFormat:@"%@",[object nome_obj]];
            annotationPoint.subtitle = [NSString stringWithFormat:@"%@",[object address]];
            [map addAnnotation:annotationPoint];
        }

        else if ([[object iden]intValue] == 4) {

            type = @"type_four";

            CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:@"latitude"] floatValue], [[[object coord]objectForKey:@"longitude"] floatValue]);

            MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
            annotationPoint.coordinate = annotationCoord;
            annotationPoint.title = [NSString stringWithFormat:@"%@",[object nome_obj]];
            annotationPoint.subtitle = [NSString stringWithFormat:@"%@",[object address]];
            [map addAnnotation:annotationPoint];
        }


这是我在地图上添加的四种对象

当我进入这种方法时

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

    MKAnnotationView *view = nil;
    if (annotation != mapView.userLocation) {

            if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 1){

                view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"type_one"];
                if (!view) {

                    view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"type_one"];
                    view.canShowCallout = YES;

                    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                    [aButton addTarget:self action:@selector(open:) forControlEvents:UIControlEventTouchUpInside];
                    [aButton setTag:countPoint+100];
                    view.rightCalloutAccessoryView = aButton;

                    view.image = [UIImage imageNamed:@"type_one.png"];
                }

            }

            else if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 2){

                view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"type_two"];
                if (!view) {

                    view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"type_two"];

                    view.canShowCallout = YES;

                    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                    [aButton addTarget:self action:@selector(open:) forControlEvents:UIControlEventTouchUpInside];
                    [aButton setTag:countPoint+100];
                    view.rightCalloutAccessoryView = aButton;

                    view.image = [UIImage imageNamed:@"type_two.png"];
                }
            }
            else if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 3){


                view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"type_three"];
                if (!view) {

                    view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"type_three"];

                    view.canShowCallout = YES;

                    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                    [aButton addTarget:self action:@selector(open:) forControlEvents:UIControlEventTouchUpInside];
                    [aButton setTag:countPoint+100];
                    view.rightCalloutAccessoryView = aButton;

                    view.image = [UIImage imageNamed:@"type_three.png"];
                }

            }
            else if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 4){

                view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"type_four"];
                if (!view) {

                    view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"type_four"];

                    view.canShowCallout = YES;

                    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                    [aButton addTarget:self action:@selector(openScheda:) forControlEvents:UIControlEventTouchUpInside];
                    [aButton setTag:countPoint+100];
                    view.rightCalloutAccessoryView = aButton;

                  view.image = [UIImage imageNamed:@"type_four.png"];

                }
            }
countPoint++;
}


问题是标记的类型与数组元素不对应...但是我不明白。
也就是说,如果标记的图像为“ type_one”,则在弹出窗口中显示为“ type_four”的“ title” ...不对应

最佳答案

您的注释需要“知道”它的类型,以便您可以在viewForAnnotation中创建正确的视图。为此,可以对MKPointAnnotation进行子类化,然后向其添加NSInteger值以存储数组索引。

请参见MKPointAnnotation add extra property,但是您将添加一个称为index的NSInteger属性。

创建注释时,请设置以下属性:

for  (int i = 0; i<arrayResults.count; i++)
{
    object = [arrayResults objectAtIndex:i];

    CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:@"latitude"] floatValue], [[[object coord]objectForKey:@"longitude"] floatValue]);

    // MKMyPointAnnotation is your MKPointAnnotation subclass
    MKMyPointAnnotation *annotationPoint = [[MKMyPointAnnotation alloc] init];
    annotationPoint.coordinate = annotationCoord;
    annotationPoint.title = [NSString stringWithFormat:@"%@",[object nome_obj]];
    annotationPoint.subtitle = [NSString stringWithFormat:@"%@",[object address]];

    annotationPoint.index = i;                                // store the index

    [map addAnnotation:annotationPoint];
}


然后在您的viewForAnnotation中,将注释转换为您的子类,然后取消引用类型:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
{
 MKMyPointAnnotation *myAnnotation = (MKMyPointAnnotation *)annotation;
 object = [arrayResults objectAtIndex:myAnnotation.index];      // <-- your index property

 switch ([[object iden] intValue])
 {
  case 1:                                 // type one
    .
    .
    .

 }

.
.
.
}

关于ios - map 中的随机MKPointAnnotation,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20858866/

10-09 07:04