当前位置: 首页 > 面试题库 >

如何在MKPointAnnotation中添加按钮?

骆雅昶
2023-03-14
问题内容

我只是试图在我的注释点添加一个细节按钮而被卡住,不幸的是,我不知道该怎么做。有人可以帮我吗?

下图显示了我想要实现的目标。谢谢!在此处输入图片说明

MapKitViewController:

import UIKit
import MapKit
import CoreLocation

class MapKitViewController: UIViewController, MKMapViewDelegate
{

let locationManager = CLLocationManager()

@IBOutlet weak var nmapView: MKMapView!
override func viewDidLoad()
{
    super.viewDidLoad()
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    let location = CLLocationCoordinate2D(
        latitude: 53.4265107,
        longitude: 14.5520357)

    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegion(center: location, span: span)
    nmapView.setRegion(region, animated: true)
    nmapView.showsPointsOfInterest = false
    nmapView.showsUserLocation = true
    displayMarkers()
}

func displayMarkers() -> Void
{
    let jsonURL: NSURL = NSURL(string: "http://jsonstring.com/")!

    var dataFromNetwork: NSData = NSData(contentsOfURL: jsonURL)!
    let json = JSON(data: dataFromNetwork)
    var jsonSize = json.count

    var todaysDate:NSDate = NSDate()
    var dateFormatter:NSDateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    var formattedDate:String = dateFormatter.stringFromDate(todaysDate)

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


    for(var i = 0; i < jsonSize; i++)
    {
        if(json[i]["rozpoczecie"].stringValue == formattedDate)
        {
            let clubID = json[i]["id_klub"].stringValue
            let annotation = MKPointAnnotation()
            let (resultSet, err) = SD.executeQuery("SELECT * FROM Clubs WHERE ID = ?", withArgs: [clubID])
            if(err != nil){println("blad")}
            else
            {
                for row in resultSet
                {
                    let name = row["Name"]?.asString()
                    let latitude = row["Latitude"]?.asDouble()
                    let longitude = row["Longitude"]?.asDouble()
                    annotation.title = name
                    var markerLatitude: Double = latitude!
                    var markerLongitude: Double = longitude!
                    let location = CLLocationCoordinate2D(latitude: markerLatitude, longitude: markerLongitude)
                    annotation.setCoordinate(location)
                    annotation.subtitle = json[i]["nazwa"].stringValue
                }
                nmapView.addAnnotation(annotation)
            }
        }
    }
}

问题答案:

您做对了,您只需要实现这些方法即可添加按钮以及标题和副标题

iOS 8和Xcode 6

 import UIKit
 import MapKit
 import CoreLocation

 class MapKitViewController: UIViewController, MKMapViewDelegate
 {
    let locationManager = CLLocationManager()
    @IBOutlet weak var nmapView: MKMapView!
    override func viewDidLoad()
    {
        super.viewDidLoad()
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        let location = CLLocationCoordinate2D(
            latitude: 53.4265107,
            longitude: 14.5520357)

        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegion(center: location, span: span)
        nmapView.setRegion(region, animated: true)
        nmapView.showsPointsOfInterest = false
        nmapView.showsUserLocation = true
        displayMarkers()
    }

    // 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 displayMarkers() -> Void
    {
        let jsonURL: NSURL = NSURL(string: "http://atnight.wtznc.com/json.php")!

        var dataFromNetwork: NSData = NSData(contentsOfURL: jsonURL)!
        let json = JSON(data: dataFromNetwork)
        var jsonSize = json.count

        var todaysDate:NSDate = NSDate()
        var dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        var formattedDate:String = dateFormatter.stringFromDate(todaysDate)

        let annotationView = MKAnnotationView()

        // Adding button here wont do anything  so remove these two lines


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

        // For adding button we have to use a method named as viewForAnnotation

        for(var i = 0; i < jsonSize; i++)
        {
            if(json[i]["rozpoczecie"].stringValue == formattedDate)
            {
                let clubID = json[i]["id_klub"].stringValue
                let annotation = MKPointAnnotation()
                let (resultSet, err) = SD.executeQuery("SELECT * FROM Clubs WHERE ID = ?", withArgs: [clubID])
                if(err != nil){println("blad")}
                else
                {
                    for row in resultSet
                    {
                        let name = row["Name"]?.asString()
                        let latitude = row["Latitude"]?.asDouble()
                        let longitude = row["Longitude"]?.asDouble()
                        annotation.title = name
                        var markerLatitude: Double = latitude!
                        var markerLongitude: Double = longitude!
                        let location = CLLocationCoordinate2D(latitude: markerLatitude, longitude: markerLongitude)
                        annotation.setCoordinate(location)
                        annotation.subtitle = json[i]["nazwa"].stringValue
                    }
                    nmapView.addAnnotation(annotation)
                }
            }
        }
    }
}

查看我的输出。

在此处输入图片说明



 类似资料:
  • 问题内容: 我有一个基于Swing的应用程序,其中包含一个。现在,我想使用唯一的行ID来更新或删除每一行。因此,我想向每行添加一个更新和删除按钮,它们具有支持ActionListener的功能。但是,我不知道如何使用NetBeans执行此操作。 问题答案: 要在列中显示按钮,您需要创建: 一个自定义渲染器以显示JButton 定制编辑器以响应鼠标单击 阅读Swing教程中有关如何使用表的部分。关于

  • 是否可以在报告中添加一个按钮,任何操作都将被修复? 示例:按钮“查看”,将显示更详细的统计信息。 备注: 我不完全理解如何制作文本元素的按钮

  • 问题内容: 如何在Android中动态添加按钮? 问题答案:

  • 我在Google和Stackoverflow上搜索过这个,但我只是没有得到给定的示例。有人能给我解释一下吗? 我想在表视图的最后一列添加一个按钮,当它被单击时,它应该触发一个侦听器并传递buttons行的对象。我只是没有从gist中得到下面的例子。github。通用域名格式: 这是我的完整当前代码: 现在,我必须创建一个的部分是可以理解的。但是如何将其分配给列呢? 我明白这一点: 但不是这个:

  • 我想在Java中添加一个按钮侦听器,这样当用户只需按下按钮时,按钮就会被按下。 我尝试将此操作监听器添加到我创建的按钮: 这就是整个功能: 然而,当我的gui出现时,我在输入输入后按“回车”键,什么也没有发生。我必须亲自点击按钮,这正是我试图避免的! 这是我创建的窗口: 我希望用户输入一个数字,然后按enter键,然后单击按钮。帮助我做错了什么?

  • 问题内容: 我想在我的 底栏布局的中间添加Facebook Messenger,例如超大按钮,但困惑如何添加。 我正在使用ahbottomnavigation库来制作我的底栏。 问题答案: 我只是试图使其简单而不是专业类型。看这里: BottomSheetLayout文件- : ActivityLayout - Output: 根据您的想法改变价值和设计。 示例仅是一个演示,并不包含OP要求的确切