无法更改地图中图像的图钉

时间:2015-10-04 18:44:24

标签: ios parse-platform mapkit swift2 cllocationmanager

我有一张地图,我根据Parse数据标记点。这工作正常。当我尝试使用自定义图像更改地图的红色图钉时,似乎一切正常但是当我在模拟器中运行应用程序时,红色图钉在那里,我的图像不会出现。我尝试了一些类似版本的代码,但结果相同。我试图在控制台中打印annotationView,似乎在返回之前将图像保存在行中。错误在哪里?我必须做什么才能正确显示自定义图像?感谢。

import UIKit
import MapKit
import CoreLocation
import Foundation


class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()
    var MapViewLocationManager:CLLocationManager! = CLLocationManager()
    var currentLoc: PFGeoPoint! = PFGeoPoint()


    override func viewDidLoad() {
        super.viewDidLoad()

        self.mapView.delegate = self

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


        self.mapView.showsUserLocation = true
    }

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

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
        let location = locations.last

        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))

        self.mapView.setRegion(region, animated: true)

        self.locationManager.stopUpdatingLocation()
    }

    override func viewDidAppear(animated: Bool) {
        let annotationQuery = PFQuery(className: "_User")
        currentLoc = PFGeoPoint(location: MapViewLocationManager.location)
        annotationQuery.whereKey("restaurant", equalTo: true)
        annotationQuery.whereKey("restaurantPosition", nearGeoPoint: currentLoc, withinMiles: 6000)
        annotationQuery.findObjectsInBackgroundWithBlock {
            (posts, error) -> Void in
            if error == nil {
                // The find succeeded.
                print("Successful query for annotations")
                let myPosts = posts as [PFObject]!

                for post in myPosts {
                    let point = post["restaurantPosition"] as! PFGeoPoint
                    let annotation = MKPointAnnotation()
                    annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)

                    self.mapViewN(self.mapView, viewForAnnotation: annotation).annotation = annotation
                    self.mapView.addAnnotation(annotation)
                }
            } else {
                // Log details of the failure
                print("Error: \(error)")
            }
        }

    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Error: " + error.localizedDescription)
    }

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

        let identifier = "pin"

    // Reuse the annotation if possible
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true
            annotationView!.image = UIImage(named: "map_marker_2")

        }
        else {
            annotationView!.annotation = annotation
        }

        print(annotationView?.image)
        return annotationView!
    }


}

2 个答案:

答案 0 :(得分:0)

是否会调用

mapViewN:viewForAnnotation委托?

也许您应该将mapViewN重命名为mapView

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

- >

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

答案 1 :(得分:0)

您的方法地图视图:viewForAnnotation:名称错误。

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

名称中的N不属于那里,因此永远不会调用您的委托方法。 (委托方法的方法名称必须完全正确,否则方法不会被调用。对于必需的委托方法,您甚至可能会崩溃。)

应该是:

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