2015-10-04 86 views
0

我有一張地圖,我根據分析數據標記點。這工作正常。當我嘗試用自定義圖像更改地圖的紅色針腳時,似乎一切正常,但是當我在模擬器中運行應用程序時,紅色針腳在那裏,我的圖像不出現。我已經嘗試了一些類似版本的代碼,結果相同。我試圖在控制檯中打印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! 
    } 


} 

回答

0

你的方法映射視圖:viewForAnnotation:被錯誤命名。你有

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

那名字中的N不屬於那裏,所以你的委託方法永遠不會被調用。 (對於委託方法,該方法的名稱必須是完全正確或方法不會被調用對於需要委託方法,你甚至可能會崩潰。)

它應該是:

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

它完美的作品! –

0

mapViewN:viewForAnnotation委託被調用?

也許你應該重新命名mapViewNmapView

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

- >

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

它絕對SHOULD是'mapView',而不是'mapViewN'。如果委託方法名稱錯誤,它將不會被調用。 –