2017-03-21 10 views
1

カスタム注釈とパーセンテージラベルを追加しました。 enter image description hereiOSのボタンをクリックしてCustomAnnotationのUILabel値を変更する方法

赤い丸でボタンを押すと、ラベルの値をパーセントからビジネス名に変更します。

マイコード:

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

    static NSString *identifier = @"MyLocation"; 
    if ([annotation isKindOfClass:[BusinessCustomAnnotation class]]) { 

     MKAnnotationView *annotationView = (MKAnnotationView *) [mapViewOffers dequeueReusableAnnotationViewWithIdentifier:identifier]; 
     if (annotationView == nil) { 
      annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 

      UILabel* category = [[UILabel alloc] initWithFrame:CGRectMake(annotationView.frame.size.width/2, 15, 55, 20)]; 

      BusinessCustomAnnotation *myAnnotationView = (BusinessCustomAnnotation *)annotation; 

      NSLog(@"Type One Offer! = %i", mapTypes); 
      [category setAdjustsFontSizeToFitWidth:YES]; 

      if (mapTypes == 1) { 
       category.text = [NSString stringWithFormat:@"%@%@", myAnnotationView.offerPercentage, @"%"]; 
      }else if (mapTypes == 2){ 
       category.text = myAnnotationView.businessName; 
      }else if (mapTypes == 3){ 
       category.text = myAnnotationView.businessName; 
      } 

      [category setMinimumScaleFactor:1.0]; 
      category.font = [UIFont systemFontOfSize:15.0 weight:5.0]; 
      category.textAlignment = NSTextAlignmentCenter; 
      [annotationView addSubview:category]; 

      annotationView.enabled = YES; 
      annotationView.canShowCallout = NO; 
      annotationView.image = [UIImage imageNamed:@"iconMapMarker"];//here we use a nice image instead of the default pins 
     } else { 
      annotationView.annotation = annotation; 
     } 

     return annotationView; }return nil; } 

のMapViewデリゲート上記は一度だけを求めています。 解決を待っています。

私を助けてくれてありがとうございます。

答えて

0

アノテーションビューとのユーザー対話を検出するには、2つの方法があります。一般的な手法は、MKAnnotationViewのコールアウト(典型的なマップアプリでピンをタップすると表示されるポップオーバーバブル)を定義することです。そして、あなたは標準viewForAnnotation方法であなたの注釈のための注釈ビューを作成します。これにより

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"]; 
    annotationView.canShowCallout = YES; 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    return annotationView; 
} 

、開示をあなたはコールアウトを取得していますが、上記の私の例では、右のアクセサリーを、追加していますインジケータ。そうすることで、彼らはあなたの注釈ビュー(上記の私の例では地図上のピン)をタップし、コールアウトを見て、そのコールアウトの右側のアクセサリー(この例では小さな開示インジケーター)をタップすると、calloutAccessoryControlTappedが呼び出されます。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
    { 
     //first check your view class here 
     // here your code for change text on view 
    } 

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    //first check your view class here 
    // here your code for change text on view 
} 
0

アノテーションを更新する必要があります。ボタンのアクションでは、これを試してみてください。

mapTypes = 2 

    for (id<MKAnnotation> annotation in mapView.annotations) 
    { 
     [mapView removeAnnotation:annotation]; 
     [mapView addAnnotation:annotation]; 
    } 
0

あなたは既に注釈pin.Toはあなたがすべてのピンを削除し、それを再度追加する必要が注釈ピンに変更を加える追加を変更することはできません。

0

注釈は更新されません。

あなたが持つすべての既存の注釈を削除する必要が

[self.mapViewのremoveAnnotations:self.mapView.annotations];

を入力し、ビジネス名を表示するために「mapTypes」変数の値を「2」または「3」に更新します。

[MKMapView addAnnotation:]を使用してアノテーションを再度追加できます。

関連する問題