2016-06-28 15 views
1

MKAnnotationView私は位置ピンのタップでタイトル、サブタイトル、情報ボタンを表示しています。MKAnnotationViewのボタンをタップしてNavigationControllerを開きます

Iしかし、このコードを使用時に以下のコード

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

     if annotation is MKUserLocation { 
      //return nil so map view draws "blue dot" for standard user location 
      return nil 
     } 


     let reuseId = "pin" 
     let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     pinView.canShowCallout = true 
     pinView.animatesDrop = true 
     pinView.pinTintColor = UIColor.darkGrayColor() 
     pinView.draggable = true 
     let btn = UIButton(type: .DetailDisclosure) 
     pinView.rightCalloutAccessoryView = btn 

     let tapGesture = UITapGestureRecognizer(target: self,action: #selector(MapView.calloutTapped(_:))) 
     pinView.addGestureRecognizer(tapGesture) 

     return pinView 
    } 

    func calloutTapped(sender: UITapGestureRecognizer) { 
     // if sender.state != UIGestureRecognizerState.Began { return } 
     let annView: MKAnnotationView! = sender.view as? MKAnnotationView 
     let ann:MKAnnotation! = annView!.annotation 
     print("handlePinButtonTap: ann.title \(ann!.title!!) and \(ann!.subtitle!!)") 
     let touchLocation = sender.locationInView(mapView) 
     let locationCoordinate = mapView.convertPoint(touchLocation, toCoordinateFromView: mapView) 
     print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude) ") 

     let storyboard : UIStoryboard = UIStoryboard(name: "ShoppingCart", bundle: nil) 
     let vc : ShoppingCartController = storyboard.instantiateViewControllerWithIdentifier("ShoppingCart") as! ShoppingCartController 


     let navigationController = UINavigationController(rootViewController: vc) 

     self.presentViewController(navigationController, animated: true, completion: nil) 



    } 

を追加して、地図上のピンのタップに、ユーザはShoppingCartストーリーボードにナビゲートされます。 ViewControllerをタップされたイベントのタイトル、サブタイトル、緯度、経度と共に情報ボタンのタップに表示したいと思います。ここで

答えて

0

は、作業溶液

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

     if annotation is MKUserLocation { 
      //return nil so map view draws "blue dot" for standard user location 
      return nil 
     } 


     let reuseId = "pin" 
     let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     pinView.canShowCallout = true 
     pinView.animatesDrop = true 
     pinView.pinTintColor = UIColor.darkGrayColor() 
     pinView.draggable = true 
     let btn = UIButton(type: .DetailDisclosure) 
     pinView.rightCalloutAccessoryView = btn 

     let tapGesture = UITapGestureRecognizer(target: self,action: #selector(MapView.calloutTapped(_:))) 
     pinView.addGestureRecognizer(tapGesture) 

     return pinView 
    } 

    func calloutTapped(sender: UITapGestureRecognizer) { 


     // if sender.state != UIGestureRecognizerState.Began { return } 
     let annView: MKAnnotationView! = sender.view as? MKAnnotationView 
     let ann:MKAnnotation! = annView!.annotation 
     print("handlePinButtonTap: ann.title \(ann!.title!!) and \(ann!.subtitle!!)") 
     let touchLocation = sender.locationInView(mapView) 
     let locationCoordinate = mapView.convertPoint(touchLocation, toCoordinateFromView: mapView) 
     print("Tapped at lat: \(locationCoordinate.latitude) long: \(locationCoordinate.longitude) ") 
} 
です
関連する問題