1

我正在尝试使用 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
    }

}
4

1 回答 1

1

根据您的描述,听起来delegate地图视图的 尚未设置。您可以通过前往 outlets 检查器在 IB 中进行设置。您可以使用以下方式以编程方式设置它:

nmapview.delegate = self
于 2015-05-31T14:25:53.570 回答