MKAn中的MKAnnotation随机顺序

时间:2012-06-14 10:58:46

标签: iphone objective-c mkmapview mkannotation

我有一个带有有序对象列表的表视图,按我和对象之间的距离排序。我需要在地图中显示注释以向用户显示对象的位置,但我需要以与表的顺序相同的顺序显示,因为然后我需要用一个按下来显示每个对象的信息。

问题在于,每当我将注释放在地图中时,注释的标记号就不同了。就像地图以随机顺序添加注释一样。

我有一个注释数组,然后我将该数组添加到地图中:

    MKPointAnnotation* tallerAnnotation = [[MKPointAnnotation alloc] init];
    [tallerAnnotation setCoordinate:CLLocationCoordinate2DMake([taller getLatitudTaller],       [taller getLongitudTaller])];
    [tallerAnnotation setTitle:[taller getNombre]];
    [tallerAnnotation setSubtitle:[taller getDomicilio]];
    [annotations addObject:tallerAnnotation];

然后将数组添加到地图中:

    [mapa addAnnotations:annotations];

以下是我自定义注释的代码:

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

    MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapa dequeueReusableAnnotationViewWithIdentifier: @"YourPinId"];
    if (pin == nil) {
    pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation_ reuseIdentifier: @"YourPinId"];


    }
    else {
    pin.annotation = annotation_;
    }
    if ([[annotation_ title]isEqualToString:@"Tu posición"])
    pin.pinColor = MKPinAnnotationColorRed;
    else
    {
    [pin setCanShowCallout:YES];
    pin.pinColor = MKPinAnnotationColorGreen;
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton addTarget:self action:@selector(getInfoTalleres:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTag:posicionActualTaller]; 
    pin.rightCalloutAccessoryView = rightButton;

    UILabel *l1=[[UILabel alloc] init];
    l1.frame=CGRectMake(0, 5, 50, 15);
    l1.backgroundColor = [UIColor clearColor];
    l1.textColor = [UIColor whiteColor];
    l1.font=[UIFont fontWithName:@"Arial Rounded MT Bold" size:(10.0)];
    l1.text=[[[mainDelegate getTalleres]objectAtIndex: posicionActualTaller]getDistancia];
    posicionActualTaller +=1;
    UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,15)];
    [leftCAV  addSubview : l1];
    pin.leftCalloutAccessoryView = leftCAV;
    }


    pin.animatesDrop = YES;
    return pin;
    }

我想做这样的事情:

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
    NSLog(@"Tag del taller = %i",[control tag]);
    [mainDelegate setTallerActual:[[mainDelegate getTalleres]objectAtIndex:[control tag]]];
    [self detalleTaller];
    }
每次进入地图视图时,

[control tag]都有不同的值,这样就不可能做[object objectAtIndex:[control tag]];

我需要帮助。

提前致谢。

好的,最后我找到了解决方案。

当我创建

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(getInfoTalleres:) forControlEvents:UIControlEventTouchUpInside];
注释的

我将注释标题分配给按钮:

[rightButton setTitle:[annotation_ subtitle] forState:UIControlStateNormal];

然后:

-(void)getInfoTalleres:(UIButton* )sender
{   
for (int i = 0; i < [[mainDelegate getTalleres]count]; i++)
{
    if ([[sender currentTitle] isEqualToString:[[[mainDelegate getTalleres]objectAtIndex:i]getDomicilio]])
    {
        [mainDelegate setTallerActual:[[mainDelegate getTalleres]objectAtIndex:i]];
        [self detalleTaller];
        break;
    }
}

}

希望有人可以使用此解决方案。

非常感谢Cocoa。

问候。

1 个答案:

答案 0 :(得分:0)

点击此处的答案,访问标题属性Determine which MKPinAnnotationView was selected?和教程http://www.scribd.com/doc/91629556/192/The-MKAnnotationView-class

如果您仍未找到答案,请告诉我