向 MKPointAnnotation 添加按钮?

Posted

技术标签:

【中文标题】向 MKPointAnnotation 添加按钮?【英文标题】:Adding a button to MKPointAnnotation? 【发布时间】:2015-04-13 16:23:26 【问题描述】:

我只写了几行代码,但在尝试向我的注释点添加详细信息按钮时卡住了,我不知道怎么做。有谁知道这是怎么做到的吗?下图显示了我想要实现的目标。谢谢!

http://i.stack.imgur.com/kK1Ox.jpg

我试图在我的注释中获得圆圈中的大 i。

现在我只有一个带有标题和副标题的注释,如下所示。

http://imgur.com/L3VMe5l.png

另外,如果你知道,当用户点击圆圈中的 i 时,我如何将他们带到新的转场?

感谢您的帮助。

import UIKit
import MapKit

class PropertyMasterViewConroller: UIViewController, MKMapViewDelegate 


    @IBOutlet weak var nMapView: MKMapView!
    @IBOutlet weak var testButton: UIButton!

    override func viewDidLoad() 
        super.viewDidLoad()

        //Location of Boston College
        let initLocation = CLLocationCoordinate2D(
            latitude: 42.335992,
            longitude: -71.167333
        )
        //Span of the Map
        let span = MKCoordinateSpanMake(0.1, 0.1)
        let region = MKCoordinateRegion(center: initLocation, span: span)
        nMapView.setRegion(region, animated: true)

        addAnnotations()

        testButton.setTitle("Test Button", forState: .Normal)

    

    override func viewWillAppear(animated: Bool) 
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"nav.png"), forBarMetrics: .Default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.translucent = true
    

    // When user taps on the disclosure button you can perform a segue to navigate to another view controller
    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) 
        if control == view.rightCalloutAccessoryView
            println(view.annotation.title) // annotation's title
            println(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! 

        println("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
        

        var button = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton // button with info sign in it

        pinView?.rightCalloutAccessoryView = button


        return pinView
    

    func addAnnotations() -> Void 

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


        //1070 Beacon
        let annotation = MKPointAnnotation()
        annotation.setCoordinate(CLLocationCoordinate2D (latitude: 42.345532,longitude: -71.109526))
        annotation.title = "1070 Beacon Street"
        annotation.subtitle = "***line, MA 02446"
        nMapView.addAnnotation(annotation)

        //1070 Beacon
        let location_18 = MKPointAnnotation()
        location_18.setCoordinate(CLLocationCoordinate2D (latitude: 42.347236,longitude: -71.15000))
        location_18.title = "18 Shepard Street"
        location_18.subtitle = "Brighton, MA 02135"
        nMapView.addAnnotation(location_18)


        //1070 Beacon
        let location_26 = MKPointAnnotation()
        location_26.setCoordinate(CLLocationCoordinate2D (latitude: 42.358663,longitude: -71.146024))
        location_26.title = "26 Lincoln Street"
        location_26.subtitle = "Brighton, MA 02135"
        nMapView.addAnnotation(location_26)

        //1070 Beacon
        let location_1280 = MKPointAnnotation()
        location_1280.setCoordinate(CLLocationCoordinate2D (latitude: 42.363639,longitude: -71.139366))
        location_1280.title = "1280 Soldiers Field Road"
        location_1280.subtitle = "Brighton, MA 02135"
        nMapView.addAnnotation(location_1280)

        //1070 Beacon
        let location_59 = MKPointAnnotation()
        location_59.setCoordinate(CLLocationCoordinate2D (latitude: 42.363147,longitude: -71.201493))
        location_59.title = "59-85 Chapel Street"
        location_59.subtitle = "Newton, MA 02458"
        nMapView.addAnnotation(location_59)

        //1070 Beacon
        let location_1 = MKPointAnnotation()
        location_1.setCoordinate(CLLocationCoordinate2D (latitude: 42.309260,longitude: -71.134556))
        location_1.title = "1 Newton Place"
        location_1.subtitle = "Newton, MA 02458"
        nMapView.addAnnotation(location_1)
    

【问题讨论】:

查看这个***.com/questions/28225296/… 故事板中地图视图的代理出口是否连接到视图控制器?如果没有,则不会调用 viewForAnnotation 等委托方法。 Johnny,当你说调用 viewForAnnotation 方法时,你的意思是这样的吗? mapView(mapView, viewForAnnotation: annotation) 另外,我应该把它放在哪里,就在评论下面 // 为了添加按钮,我们必须使用一个名为 viewForAnnotation 的方法?安娜,你是什么意思地图视图代表出口?我只是控制从情节提要上的 MKMapView 单击到我的视图控制器并将其添加为插座。 @emaadali,请参阅此问题中的图片:***.com/questions/9056535/…。委托出口(圆形图标)应该连接(填充)到视图控制器——不仅仅是 mapView 出口。此外,您不调用 viewForAnnotation。地图视图本身会调用它(但前提是它的委托已设置)。 【参考方案1】:

我无权访问您的第二个图片网址,但我假设您正在查看以下内容。如果没有,请在下面发表评论。

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

        if control == annotationView.rightCalloutAccessoryView 
            performSegueWithIdentifier("Detail", sender: self)
        
    

您还可以参考以下示例:

MKPointAnnotations touch event in swift

MapKit in Swift, Part 2

http://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial

【讨论】:

嘿,第二张图片在这里:imgur.com/L3VMe5l 问题主要是我还没有注释中的披露按钮。我只是无法显示按钮。

以上是关于向 MKPointAnnotation 添加按钮?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 MKPointAnnotation 不是自定义的?

MKPointAnnotation 到 MKMapItem

MKPointAnnotation 不再在 iOS 11 中显示标题

MKPointAnnotation 在标签中显示图像

获取 MKPointAnnotation 标题

拖动时更新 MKPointAnnotation 对象