2013-10-22 109 views
1

我有一个条件语句来添加地图注释图标/针脚在下面的方法。我遇到的问题是地图上正在填充所有相同的图标。它应该检测到cat id并根据检测到的cat id显示图标。我不确定是什么问题,因为这在iOS 6中有效,现在在iOS 7中,地图仅显示所有相同的注释图标图像。地图注释显示所有点的所有图像/针脚

- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation { 
annView = nil; 
if(annotation != mapingView.userLocation) 
{ 

    static NSString *defaultPinID = @""; 
    annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
    if (annView == nil) 
     annView = [[MKAnnotationView alloc] 
        initWithAnnotation:annotation reuseIdentifier:defaultPinID] ; 


    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 

    annView.rightCalloutAccessoryView = rightButton; 

    MyAnnotation* annotation= [MyAnnotation new]; 

    annotation.catMapId = categoryIdNumber; 
    NSLog(@"categoryIdNumber %@",categoryIdNumber); 
    NSLog(@"annotation.catMapId %@",annotation.catMapId); 


     if (annotation.catMapId == [NSNumber numberWithInt:9]) { 
      annView.image = [UIImage imageNamed:@"PIN_comprare.png"]; 

      NSLog(@"annview 9"); 

     } 

     else if (annotation.catMapId == [NSNumber numberWithInt:10]) { 
      annView.image = [UIImage imageNamed:@"PIN_mangiare.png"]; 

      NSLog(@"annview 10"); 

     } 

     else if (annotation.catMapId == [NSNumber numberWithInt:11]) { 
      annView.image = [UIImage imageNamed:@"PIN_visitare.png"]; 

      NSLog(@"annview 11"); 

     } 

     else if (annotation.catMapId == [NSNumber numberWithInt:12]) { 
      annView.image = [UIImage imageNamed:@"PIN_vivere.png"]; 

      NSLog(@"annview 12"); 

     } 

    annView.canShowCallout = YES; 

} 

return annView; 

}

enter image description here

+0

http://stackoverflow.com/questions/10898122/map-view-annotations-with-different-pin-colors 工程! – user2883540

回答

1

如果像你说的, “这没有工作在iOS 6中”,你应该考虑它相当于幸运的是,它确实(或似乎),并且这种设置注释图像的方法不应该依赖于任何版本。

尽管@Ar Ma是正确的,应该设置注解视图的annotation属性(万一视图正在被重用),这并不能解决主要问题。

注释视图的image基于的categoryIdNumber这似乎是一些变量viewForAnnotation委托方法的值被设置。

不能假设:

  1. viewForAnnotation会打电话addAnnotation之后立即调用。即使在iOS 6或更早的版本中,这也不能保证。
  2. viewForAnnotation对于每个注释只会被调用一次。对于与用户平移或缩放地图相同的注释,可以多次调用委托方法,并将注释返回到屏幕上。
  3. viewForAnnotation将按照添加注释的顺序调用。这是点1的结果和2

我假设你叫addAnnotation之前,该categoryIdNumber设置正确,然后根据上面的不正确的假设,viewForAnnotation使用categoryIdNumber设置图像。

正在发生的事情是,viewForAnnotation正在被之后的某个全部或部分addAnnotation调用的地图视图称为完成在这一点categoryIdNumber可能是添加了最后的注解连接的价值和所有注释使用应用图像到最后一个注释。


为了解决这个问题(的iOS版本,无论),你必须把正确的categoryIdNumber值到每个注释对象调用addAnnotation之前。

它看起来像您的注释类是MyAnnotation,并且您已经有一个catMapId属性。不viewForAnnotation方法,它是为时已晚 -

您必须调用addAnnotation前的注释设置该属性。 (顺便说一下,你正在创建一个MyAnnotation对象viewForAnnotation方法,它是没有意义的。)


那么,您创建并添加注释(不viewForAnnotation):

MyAnnotation* myAnn = [[MyAnnotation alloc] init]; 
myAnn.coordinate = ... 
myAnn.title = ... 
myAnn.catMapId = categoryIdNumber; // <-- set catMapId BEFORE addAnnotation 
[mapView addAnnotation:myAnn]; 

然后在viewForAnnotation代码应该是这样的:

- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    annView = nil; 
    if(annotation != mapingView.userLocation) 
    { 

     static NSString *defaultPinID = @"MyAnnId"; 
     annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
     if (annView == nil) 
     { 
      annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ; 
      annView.canShowCallout = YES; 
     } 
     else 
     { 
      //view is being re-used, re-set annotation to current... 
      annView.annotation = annotation; 
     } 

     UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 

     annView.rightCalloutAccessoryView = rightButton; 


     //Make sure we have a MyAnnotation-type annotation 
     if ([annotation isKindOfClass:[MyAnnotation class]]) 
     { 
      //Do not CREATE a local MyAnnotation object here. 
      //Instead, get the catMapId from the annotation object 
      //that was PASSED INTO the delegate method. 
      //MyAnnotation* annotation= [MyAnnotation new]; 
      //annotation.catMapId = categoryIdNumber; 

      MyAnnotation *myAnn = (MyAnnotation *)annotation; 

      //The value of the external variable categoryIdNumber is irrelevant here. 
      //NSLog(@"categoryIdNumber %@",categoryIdNumber); 

      NSLog(@"myAnn.catMapId %@",myAnn.catMapId); 


      //Put the NSNumber value into an int to simplify the code below. 
      int myAnnCatMapId = [myAnn.catMapId intValue]; 

      NSString *imageName = nil; 
      switch (myAnnCatMapId) 
      { 
       case 9: 
       { 
        imageName = @"PIN_comprare.png"; 
        break; 
       } 

       case 10: 
       { 
        imageName = @"PIN_mangiare.png"; 
        break; 
       } 

       case 11: 
       { 
        imageName = @"PIN_mangiare.png"; 
        break; 
       } 

       case 12: 
       { 
        imageName = @"PIN_vivere.png"; 
        break; 
       } 

       default: 
       { 
        //set some default image for unknown cat ids... 
        imageName = @"default.png"; 
        break; 
       } 
      } 

      annView.image = [UIImage imageNamed:imageName]; 

      NSLog(@"annview %d", myAnnCatMapId); 
     } 
    } 

    return annView; 
} 
+0

这工作得很好!感谢您的时间帮助我:) – user2588945

+0

不客气。您可能已经解决了这个问题,但是我在switch语句中犯了一个小错误。对于案例11,它应该是visitare的形象。 – Anna

1

末加入这一行:

annView.annotation = annotation; 
+0

谢谢。注释确实显示,但它们都是相同的针图像。它应该是针对不同类别ID的不同针图像。 – user2588945

+0

我添加了一个显示引脚的屏幕截图,但它们都是相同的引脚,并且应该根据条件各不相同。 – user2588945

0

同样的麻烦,我的决定是不要使用

pinView.animatesDrop = YES; 

自定义图标只是不适用于我的动画下降。

相关问题