首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKPointAnnotation添加按钮

MKPointAnnotation添加按钮
EN

Stack Overflow用户
提问于 2018-04-07 04:09:47
回答 1查看 256关注 0票数 0

我正在尝试添加一个按钮,然后在MKPointAnnotations中添加一个新的视图控制器

我仔细研究了这两个问题

How to add button to MKPointAnnotation in Swift

How to add a button to the MKPointAnnotation?

他们两个都有

代码语言:javascript
复制
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)
-> MKAnnotationView? {

作为解决方案的一部分,但当我实现这个函数时,它不会被调用(我在这里假设它是自动调用的,就像swift中的表视图或集合视图中的单元格一样)。我对下一步应该尝试什么的洞察力将是令人惊讶的,因为总的来说,我觉得这应该很容易,我遗漏了一些基本的东西。

代码语言:javascript
复制
import UIKit
import Foundation
import MapKit
import CoreLocation

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    var screenWidth : CGFloat = 0.0
    var screenHeight : CGFloat = 0.0

    var mapView = MKMapView()

    var annotations : [MKPointAnnotation] = []

    var annotationView = MKAnnotationView()

    let lm = CLLocationManager()

    let regionRadius: CLLocationDistance = 1000

    var flag : Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()

        screenWidth = UIScreen.main.bounds.width
        screenHeight = UIScreen.main.bounds.height

        mapView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
        self.view.addSubview(mapView)
        lm.delegate = self
        lm.desiredAccuracy = kCLLocationAccuracyBest
        lm.requestWhenInUseAuthorization()
        lm.startUpdatingLocation()


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

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let coordinateRegion = MKCoordinateRegionMakeWithDistance(locations[locations.count-1].coordinate, regionRadius * 0.5, regionRadius * 0.5)
            mapView.setRegion(coordinateRegion, animated: true)


        if flag == false {
            addAnnotation(latitude: locations[locations.count-1].coordinate.latitude, longitude: locations[locations.count-1].coordinate.longitude)
            flag = true
        }
    }

    func addAnnotation(latitude: Double, longitude: Double){
        let annotation = MKPointAnnotation()
        annotation.coordinate.latitude = latitude
        annotation.coordinate.longitude = longitude
        annotation.title = "here"

        annotations.append(annotation)

        mapView.addAnnotation(annotations[0])
    }



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

        print("fuddddge not working")

        let pinView = MKPinAnnotationView()
        return pinView

    }



    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
EN

回答 1

Stack Overflow用户

发布于 2018-04-07 04:45:37

添加MKAnnotation类型的注释时会调用此方法,因此应首先创建MKAnnotation的子类

代码语言:javascript
复制
//  add them inside MKAnnotation class below import

func == (left: PinColor, right: PinColor) -> Bool
{
  return left.rawValue == right.rawValue
}
enum PinColor : String
{
    case Blue = "Blue"
    case Red = "Red"
    case Green = "Green"
    case Purple = "Purple"

    /* We convert our pin color to the system pin color */
    func toPinColor() -> MKPinAnnotationColor
    {
        switch self
        {
        case .Red:
            return .red
        case .Green:
            return .green
        case .Purple:
            return .purple
        default:
            return .red
        }
    }
}


class MyAnnotation: NSObject, MKAnnotation
{
  var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0)
  var title: String!
  var subtitle: String!
  var pinColor: PinColor!

  init(coordinate: CLLocationCoordinate2D, title: String,
     subtitle: String,
     pinColor: PinColor)
  {
     self.coordinate = coordinate
     self.title = title
     self.subtitle = subtitle
     self.pinColor = pinColor
     super.init()
  }

}

然后添加注释

代码语言:javascript
复制
let blueLocation = CLLocationCoordinate2D(latitude: ..... , longitude: .... )

let blueAnnotation = MyAnnotation(coordinate: blueLocation, title:"",subtitle: "",pinColor: .Green) 

self.myMapView.addAnnotation(blueAnnotation)

//

代码语言:javascript
复制
func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
    if annotation is MyAnnotation == false
    {

        return nil
    }

    let senderAnnotation = annotation as! MyAnnotation

    let pinReusableIdentifier = senderAnnotation.pinColor.rawValue

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinReusableIdentifier)

    if annotationView == nil
    {            
        annotationView = MKAnnotationView(annotation: senderAnnotation,                                                                                                reuseIdentifier: pinReusableIdentifier)
        annotationView!.canShowCallout = true
    } 

    if senderAnnotation.pinColor == PinColor.Green
    {
         let pinImage = UIImage(named:"name.png")

         annotationView!.image = pinImage

    }

    return annotationView 

 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49700344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档