1

カスタム Callout を作成しようとしていますが、何らかの理由で既定のポイント イメージが変更されません。

Parse を使用してポイントのデータを取得しています。MKAnnotationView から継承するカスタム クラス (customAnnotationView) を作成し、mapView 関数もオーバーライドしています。

ビューコントローラーは次のとおりです。

    import UIKit
    import Parse
    import MapKit
    import CoreLocation

    class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{

    @IBOutlet weak var map: MKMapView!
    var locationManager = CLLocationManager()


    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate=self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

        var annotationQuery = PFQuery(className: "Doc")
        annotationQuery.findObjectsInBackgroundWithBlock {
            (Doc, error) -> Void in
            if error == nil {
                // The find succeeded.
                print("Successful query for annotations")
                let docs = Doc
                for doctor in docs! {
                    let point = doctor["location"] as! PFGeoPoint
                    let annotation = MKPointAnnotation()
                    annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
                    annotation.title = String(doctor["name"])+String(doctor["surname"])
                    annotation.subtitle = String(doctor["address"])
                   print(doctor)
                    self.map.addAnnotation(annotation)
                }
            } else {
                // Log details of the failure
                print("Error: \(error)")
            }
        }
    }


    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        var userLocation:CLLocation = locations[0]
        var userLangtitude =  userLocation.coordinate.latitude
        var userLongtitude =  userLocation.coordinate.longitude

        // Initialize Map
        var latidude:CLLocationDegrees = userLangtitude
        var longtitude:CLLocationDegrees = userLongtitude
        var latDelta:CLLocationDegrees = 2
        var lonDelta:CLLocationDegrees = 2
        var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latidude,longtitude)
        var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
        self.map.setRegion(region, animated: true)
        self.map.showsUserLocation = true;
    }


    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        // If annotation is user current location do not change the annotation
        if annotation.isKindOfClass(MKUserLocation) {
            return nil
        }

        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("customAnnotationView")

        if annotationView == nil{
            annotationView = customAnnotationView(annotation: annotation, reuseIdentifier: "customAnnotationView")
            annotationView?.canShowCallout = true
        }
        else{
            annotationView?.annotation = annotation
        }
        return annotationView
    }

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

customAnnotationView : _

import UIKit
import MapKit
import CoreLocation
import Parse

class customAnnotationView: MKAnnotationView {


    override init(frame: CGRect) {
    super.init(frame: frame)
    }

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)

        var frame = self.frame
        frame.size = CGSizeMake(80,80)
        self.frame = frame
        self.backgroundColor = UIColor.clearColor()
        self.centerOffset = CGPointMake(-5, -5)
    }

    required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    }

    override func drawRect(rect: CGRect) {
        UIImage(named: "icon.png")?.drawInRect(CGRectMake(30,30, 30, 30))
    }

}

どんな助けでも大歓迎です! ありがとう !

4

0 に答える 0