MapKit注释按钮未显示

时间:2015-05-29 23:36:26

标签: ios swift parse-platform mkannotation mkpointannotation

我正在尝试使用Swift为iOS 8构建应用程序,该应用程序使用Parse.com数据库在MapView上显示引脚。我已经成功地将地图上的所有引脚加载到PFGeoPoints,但是我试图在每个引脚上添加一个公开按钮,这将执行一个segue来显示额外的信息。

我已经仔细检查了我的代码,但我错过了什么,是否有人注意到问题?

import UIKit
import Foundation
import Parse
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

let locationManager = CLLocationManager()
var userLocationParse = PFGeoPoint(latitude: 47.49, longitude: 19.06)

@IBOutlet weak var nmapview: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    let location = CLLocationCoordinate2D(latitude: 47.49, longitude: 19.06)

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

}

func displayMarkers() -> Void {

    //GET PIN DATA HERE

    var query = PFQuery(className: "Places")
    query.whereKey("PlaceLocation", nearGeoPoint: userLocationParse)
    query.limit = 30

    let foundPlaces = query.findObjects()

    //GETTING PFGEOLOCATIONS AND PUTTING THEM ON MAP AS ANNOTATIONS
    //Loading pin details

    var annotationQuery = PFQuery(className: "Places")
    annotationQuery.whereKey("PlaceLocation", nearGeoPoint: userLocationParse)
    annotationQuery.findObjectsInBackgroundWithBlock {
        (posts, error) -> Void in
        if error == nil {
            // The find succeeded.
            //println("Successful query for annotations")
            let myPosts = posts as! [PFObject]

            for post in myPosts {


                let pinAnnotation = PinAnnotation()
                let point = post["PlaceLocation"] as! PFGeoPoint
                let pointName = post["PlaceName"] as! String
                let pointDetails = post["PlaceDetails"] as! String
                let thePinsLocation = CLLocationCoordinate2DMake(point.latitude, point.longitude)

                pinAnnotation.setCoordinate(thePinsLocation)
                pinAnnotation.title = pointName
                pinAnnotation.subtitle = pointDetails


                self.nmapview.addAnnotation(pinAnnotation)

            }
        } else {
            // Log details of the failure
            // println("Error: \(error)")
        }
    }

}

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

    if annotation is PinAnnotation {

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

        let reuseID = "myPin"

        var pinAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView

        if pinAnnotationView == nil {

            pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
            pinAnnotationView!.pinColor = .Purple
            pinAnnotationView!.canShowCallout = true
            pinAnnotationView!.animatesDrop = true

            pinAnnotationView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIButton

        } else {
            pinAnnotationView!.annotation = annotation
        }

        return pinAnnotationView
    }

    return nil
}

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if control == view.rightCalloutAccessoryView{
       performSegueWithIdentifier("infoViewController", sender: self)
    }
}

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

我还为" PinAnnotation"创建了一个类。用于销钉。不确定是否多余,但有关该主题的一些教程在必要时提出了它。

import UIKit
import MapKit
import UIKit

class PinAnnotation: NSObject, MKAnnotation {

    private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 90.0, longitude: 0.0)

    var coordinate: CLLocationCoordinate2D {
        get {
            return coord
        }
    }

    var title: String = "North Pole"
    var subtitle: String = "Santa's house"

    func setCoordinate(newCoordinate: CLLocationCoordinate2D) {
        self.coord = newCoordinate
    }

}

1 个答案:

答案 0 :(得分:2)

根据您描述的内容,听起来似乎没有设置地图视图的delegate。您可以通过前往出口检查员在IB中进行设置。您可以使用以下命令以编程方式设置它:

nmapview.delegate = self