使用Swift添加信息气泡到pin和点击方向

时间:2016-08-20 10:24:48

标签: swift2 mkmapview mapkit mkannotation mkannotationview

我试图找出当我点击地图上的图钉时如何在注释视图中放置信息气泡。

在视图中加载我加载此代码,向我显示地图

        let latDelta:CLLocationDegrees = 0.01

        let longDelta:CLLocationDegrees = 0.01


        let evntLat : NSString = venueLat
        let evntLng : NSString = venueLng
        let latitute1:CLLocationDegrees =  evntLat.doubleValue
        let longitute2:CLLocationDegrees =  evntLng.doubleValue


        let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
        let pointLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitute1, longitute2)


        let region:MKCoordinateRegion = MKCoordinateRegionMake(pointLocation, theSpan)
        mappy.setRegion(region, animated: true)

        let pinLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitute1, longitute2)
        let objectAnnotation = MKPointAnnotation()
        objectAnnotation.coordinate = pinLocation
        objectAnnotation.title = "\(self.venumeNam)"
        self.mappy.addAnnotation(objectAnnotation)

有没有办法在这个注释视图中添加一个按钮? 我试过这个

let annotationView = MKAnnotationView()
        let detailButton: UIButton = UIButton(type:UIButtonType.DetailDisclosure) as UIButton
        annotationView.rightCalloutAccessoryView = detailButton

但我无法弄清楚如何在我的代码上实现它,以便它可以显示它。 如果有人点击它,我怎么能把这个泡泡和配置功能?

仅供参考,我在这里只显示一个针脚!

this回复

更新
 class detailsViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mappy: MKMapView!
    // Here we add disclosure button inside annotation window
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        if control == view.rightCalloutAccessoryView{
            print(view.annotation!.title) // annotation's title
            print(view.annotation!.subtitle) // annotation's subttitle

            //Perform a segue here to navigate to another viewcontroller
            // On tapping the disclosure button you will get here
        }
    }

    // Here we add disclosure button inside annotation window
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

        print("viewForannotation")
        if annotation is MKUserLocation {
            //return nil
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

        if pinView == nil {
            //println("Pinview was nil")
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
        }

        let button = UIButton(type: .DetailDisclosure) as UIButton // button with info sign in it

        pinView?.rightCalloutAccessoryView = button


        return pinView
    }



    func displayMarkers() -> Void
    {

        let latDelta:CLLocationDegrees = 0.01

        let longDelta:CLLocationDegrees = 0.01


        let evntLat : NSString = venueLat
        let evntLng : NSString = venueLng
        let latitute1:CLLocationDegrees =  evntLat.doubleValue
        let longitute2:CLLocationDegrees =  evntLng.doubleValue


        let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
        let pointLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitute1, longitute2)


        let annotationView = MKAnnotationView()



        let region:MKCoordinateRegion = MKCoordinateRegionMake(pointLocation, theSpan)
        mappy.setRegion(region, animated: true)

        let pinLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitute1, longitute2)
        let objectAnnotation = MKPointAnnotation()
        objectAnnotation.coordinate = pinLocation
        objectAnnotation.title = "\(self.venumeNam)"

        self.mappy.addAnnotation(objectAnnotation)

    }

   } 

0 个答案:

没有答案