1

Parse.com データベースを使用して MapView にピンを表示する Swift を使用して、iOS 8 用のアプリを構築しようとしています。PFGeoPoints を使用してマップ上のすべてのピンを読み込むことに成功しましたが、セグエを実行して追加情報を表示する開示ボタンを各ピンに追加しようとしています。

コードを再確認しましたが、何か不足しています。問題に気付いた人はいますか?

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、マップビューの が設定されていないようです。アウトレットインスペクターに移動して、IBで設定できます。次のようにプログラムで設定できます。

nmapview.delegate = self
于 2015-05-31T14:25:53.570 に答える