2015-11-20 76 views
2

由於某些奇怪的原因,viewForAnnotation僅適用於在viewDidLoad(這是測試針腳)中設置的針腳。在別處加載的引腳在按下時不會被註釋。我已經設置了委託。我認爲它與mapView調用中的標識符有關?但我不確定如何解決它。任何幫助表示讚賞!謝謝!iOS:MapView註釋未顯示針腳

這裏是我的代碼:

import Foundation 
import UIKit 
import MapKit 
import CoreLocation 
import Alamofire 

class MapViewController: UIViewController, MKMapViewDelegate { 

var locationManager:CLLocationManager = CLLocationManager() 


@IBOutlet weak var potholeMapView: MKMapView! 

var listData: Array<String> = [] 
var idData: Array<Int> = [] 
var descriptionData: Array<String> = [] 

var latitudeData:Array<Double> = [] 
var longitudeData:Array<Double> = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    potholeMapView.delegate = self 
    locationManager.requestWhenInUseAuthorization() 
    potholeMapView!.region = sanDiegoCountyLocation() 
    potholeMapView!.mapType = MKMapType.Standard 
    potholeMapView!.showsUserLocation = true 
    potholeMapView!.showsTraffic = true 
    print(potholeMapView!.userLocationVisible) 

    // WORKING HERE ACCESSORY VIEW SHOWS 
    let encinitas = CLLocationCoordinate2DMake(32.955, -117.2459) 
    let marker = AnnotatedLocation(
     coordinate: encinitas, 
     title: "There", 
     subtitle: "You are not here") 
    potholeMapView!.addAnnotation(marker) 

} 

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    //HERE ACCESSORY VIEWS DONT SHOW 
    loadPotholeData() 
} 


func sanDiegoCountyLocation()-> MKCoordinateRegion { 
    let center = CLLocationCoordinate2DMake(32.76572795, -117.07319880) 
    let widthMeters:CLLocationDistance = 100 
    let heightMeters:CLLocationDistance = 1000*120 
    return MKCoordinateRegionMakeWithDistance(center, widthMeters, heightMeters) 
} 

func loadPotholeData(){ 
    let url = "http://bismarck.sdsu.edu/city/fromDate" 
    let parametersGet = ["type" : "street", "user" : "008812"] 
    Alamofire.request(.GET, url, parameters: parametersGet) 
     .responseJSON { response in 
      if let dataGet = response.result.value { 
       let dataDict:NSArray = dataGet as! NSArray 
       for item in dataDict{ 
        let descrip = item["created"] 
        self.listData.append(descrip!! as! String) 
        let ids = item["id"] 
        self.idData.append(ids! as! Int) 
        let description = item["description"] 
        self.descriptionData.append(description!! as! String) 
        let latitude = item["latitude"] 
        self.latitudeData.append(latitude as! Double) 
        let longitude = item["longitude"] 
        self.longitudeData.append(longitude as! Double) 
       } 
      } 
      else { 
       print("There was some error getting data") 
      } 
    } 
    createAllPins() 
} 

func createAllPins(){ 
    for (x, y) in zip(self.latitudeData, self.longitudeData) { 
     let location = CLLocationCoordinate2DMake(x, y) 
     let marker = AnnotatedLocation(
      coordinate: location, 
      title: "", 
      subtitle: "") 
     potholeMapView!.addAnnotation(marker) 
    } 
} 

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if let annotation = annotation as? AnnotatedLocation { 
     let identifier = "pin" 
     var view: MKPinAnnotationView 
     if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 
      as? MKPinAnnotationView { 
       dequeuedView.annotation = annotation 
       view = dequeuedView 
     } else { 
      view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      //view = MKPinAnnotationView(annotation: <#T##MKAnnotation?#>, reuseIdentifier: <#T##String?#>) 
      view.canShowCallout = true 
      view.calloutOffset = CGPoint(x: -5, y: 5) 
      view.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) 
     } 
     return view 
    } 
    return nil 
} 


func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    self.performSegueWithIdentifier("pushAnnotation", sender: view) 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if let identifier = segue.identifier { 
     switch identifier { 
      case "pushAnnotation": 
       let nextVC = segue.destinationViewController as! MapAnnotationDetailViewController 
       //nextVC. 
      default: break 
     } 
    } 
} 

} 

回答

6

看來,那是因爲你對其他的標題和副標題都是空白。我測試了沒有任何引號和添加內容,似乎解決了這個問題。

+0

哇,我簡直不敢相信那是-_-謝謝! @Fumbles – Ybarra

+0

看起來,當註釋添加到地圖時,.title必須至少有一個字符,即使它是「」。否則,即使.title隨後被修改爲非「」,標籤也不會顯示。 – rockhammer