【问题标题】:Telling the difference between UserLocation Pin and user added pins区分 UserLocation Pin 和用户添加的 pin
【发布时间】:2013-04-18 03:14:50
【问题描述】:

我正在尝试解决一个我不知道如何解决的问题。在我的应用程序中,当 mapkit 启动时,我在用户的当前位置放置了一个图钉。 mapkit 委托给 viewForAnnotation,它设置了 pin 的颜色,效果很好。

我遇到的问题是我无法区分注释,因此我可以将不同的颜色应用于“用户添加的位置”与我的“当前位置”图钉。我想添加一个按钮,以便他们可以删除他们添加的任何图钉,但不能删除他们的“当前位置”图钉。我似乎无法弄清楚如何提取任何可识别的信息,例如图钉的标题或副标题。

提前感谢您的帮助。

这是我的代码...

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    NSLog(@"OAAssignmentGPSViewController.m : mapView viewForAnnotation");

    if([annotation isKindOfClass: [MKUserLocation class]])
    return nil;

    NSLog(@" mapView.userLocation.title = %@", self.mapView.userLocation.title);
    static NSString* annotationIdentifier = @"currentlocation";
    MKPinAnnotationView *myPinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
    if (!myPinView) {
    myPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] ;
    myPinView.pinColor = MKPinAnnotationColorRed;
    myPinView.canShowCallout = YES;
    myPinView.animatesDrop = YES;

    NSLog(@"mapView.description %@", self.mapView.userLocation.title);
    if( annotation   != self.mapView.userLocation)<<<<<----how do I check for UserLocation
    {
        myPinView.pinColor = MKPinAnnotationColorGreen;
        myPinView.rightCalloutAccessoryView =   [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }
   }
    return myPinView;
}

【问题讨论】:

    标签: ios annotations mkannotationview mkpinannotationview


    【解决方案1】:

    为其他新手更新这个,这就是答案......

    if ([annotation isKindOfClass:[MKUserLocation class]]) ....{
    
       this is the current location pin
    
    }else {
    
        these are you custom locations pins
    }
    

    【讨论】:

      【解决方案2】:

      我唯一能想到的就是继承 MKPinAnnotationView 并添加一个简单的属性来索引它。在创建引脚时将它们添加到数组中并增加索引号。然后,对于您的“删除所有引脚”方法,您可以检查引脚的 index 属性以确保它不是第一个:

      -(IBAction)deleteAllPins
      {
         for (id pin in self.pinsArray) {
            if (pin.indexNumber >= 1) { //This is not the first pin...
               // Delete the pin
          }
      
        }
      }
      

      无论如何,这就是我会尝试的。对不起,我没有时间亲自测试它。希望它能让你开始。祝你好运。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-10
        • 2018-08-08
        • 1970-01-01
        • 2019-07-27
        • 1970-01-01
        相关资源
        最近更新 更多