【问题标题】:MapKit annotation button not showing upMapKit 注释按钮未显示
【发布时间】:2015-05-29 23:36:25
【问题描述】:

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

}

【问题讨论】:

  • 我建议停用 PinAnnotation(默认为北极?!?)。您应该只使用MKPointAnnotation,除非您需要具有特殊功能的注释(例如自定义属性)。至于为什么这不起作用,究竟发生了什么?你看到你的紫色别针还是红色别针?您是否设置了地图视图的delegate?即,您的viewForAnnotation 是否会被调用?
  • 我看到默认的红色引脚出现,与默认值没有任何变化。并且 viewForAnnotation 似乎根本没有被调用。您如何建议以编程方式设置委托?

标签: ios swift parse-platform mkannotation mkpointannotation


【解决方案1】:

根据您的描述,好像地图视图的delegate 没有设置。您可以通过前往网点检查员在 IB 中进行设置。您可以通过以下方式以编程方式设置它:

nmapview.delegate = self

【讨论】:

  • 感谢 Rob 修复了它!不敢相信它是如此简单。
  • 也感谢您在这件事上的帮助:)
猜你喜欢
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
相关资源
最近更新 更多