2015-07-03 31 views
2

我有一張地圖,我已經完美地添加了12條註解,我想要的是當有人點擊這些註解時,它會在註釋右邊有一個信息按鈕點擊時會顯示地圖應用程序中註釋的路線。我已經寫了函數來打開地圖應用程序與所需的座標我只是不知道如何去添加信息按鈕的註釋,並使其在點擊時執行該功能。MKAnnotation水龍頭上的Swift/iOS MapKit功能

編輯:我需要這個在Swift中完成。

回答

5

可以使用ViewForAnnotation方法MapView的 ,如下

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"]; 
    annotationView.canShowCallout = YES; 
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    return annotationView; 
} 

也可以呼叫附屬視圖

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    [self performSegueWithIdentifier:@"DetailsIphone" sender:view]; 
} 

這樣你就可以瀏覽到信息按鈕方向add方法。

更新了雨燕代碼

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, 
      calloutAccessoryControlTapped control: UIControl!) { 

    if control == view.rightCalloutAccessoryView { 
     println("Disclosure Pressed! \(view.annotation.subtitle)" 
    } 

} 

,你可以用這個迅速語言。

請添加該代碼viewforAnnotation:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
     if !(annotation is CustomPointAnnotation) { 
      return nil 
     } 

     let reuseId = "test" 

     var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
     if anView == nil { 
      anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      anView.canShowCallout = true 

      anView.rightCalloutAccessoryView = UIButton.buttonWithType(.InfoDark) as UIButton 

     } 
     else { 
      anView.annotation = annotation 
     } 



     let cpa = annotation as CustomPointAnnotation 
     anView.image = UIImage(named:cpa.imageName) 

     return anView 
    } 

確保您已經添加了 「MKMapViewDelegate」

+0

謝謝,但我相信上面的代碼是用於Obj-C的,我很快就在尋找。 – user4011770

+0

檢查更新的代碼 – BKjadav

+0

我已經添加了這段代碼,它可以工作,但是我看不到任何更改或信息按鈕? – user4011770

2

對於斯威夫特2我就是這麼做的:

let buttonType = UIButtonType.InfoDark 
barAnnotView?.rightCalloutAccessoryView = UIButton(type: buttonType)