2014-10-18 25 views
4

我不明白如何更改Swift中標記點的圖像。我已經嘗試了一些變化如何更改座標的註釋圖像?

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

但它似乎永遠不會被調用,我不知道如何強制它被調用。

我會說我也以編程方式添加了MKMapView,所以我可以根據使用的iPhone切換大小(水平和垂直)。 (我仍然無法相信這不是直觀的Xcode和程序員必須圍繞它進行編程。)

這裏是我的代碼:

@IBAction func addStroke(sender: UIButton) { 
    NSLog("Add Stroke Touched") 
    NSLog("X %f Y %f", manager.location.coordinate.latitude, manager.location.coordinate.longitude) 
    GPSspot = manager.location.coordinate 

    annotation.setCoordinate(GPSspot) 
    annotation.title = "Stroke" 

    let useID = "test" 

    var myAnnotation = mapHole.dequeueReusableAnnotationViewWithIdentifier(useID) 
    if (myAnnotation == nil) { 
     myAnnotation = MKAnnotationView(annotation: annotation, reuseIdentifier: useID) 
     myAnnotation.image = UIImage(named: "ballSmall.png") 
     myAnnotation.canShowCallout = true 
     myAnnotation.enabled = true 
    } else { 
     myAnnotation.annotation = annotation 
    } 

    mapHole.viewForAnnotation(annotation) 
    mapHole.addAnnotation(annotation) 
} 

//MARK: - Map Kit 
var mapRect = MMRect(xLoc: 502, yLoc:20, yWidth:218, xHeight:386) 
var mapHole:MKMapView! //= MKMapView() as MKMapView 
var annotation = MKPointAnnotation() 
var MAView:MKAnnotationView! 

//MARK: - GPS Locations 
var manager:CLLocationManager! 
var GPSspot:CLLocationCoordinate2D! 
var span:MKCoordinateSpan! 
var region:MKCoordinateRegion! 

//MARK: - Default Initializer 
override func viewDidLoad() { 
    super.viewDidLoad() 

    //MARK: Locations 
    manager = CLLocationManager() 
    if (CLLocationManager.locationServicesEnabled()) { 
     NSLog("locations services started") 
     manager.delegate = self 
     manager.desiredAccuracy = kCLLocationAccuracyBest 
     manager.requestAlwaysAuthorization() 
     manager.requestWhenInUseAuthorization() 
     manager.startUpdatingLocation() 
    } else { 
     NSLog("location services failed") 
    } 


    //MARK: MKMapView 
    mapHole = MKMapView(frame: mapRect.textRect) 
    span = MKCoordinateSpanMake(0.001, 0.001) 
    GPSspot = manager.location.coordinate 
    region = MKCoordinateRegion(center: GPSspot, span: span) 
    mapHole.setRegion(region, animated: true) 
    mapHole.mapType = MKMapType.Satellite 
    mapHole.showsUserLocation = true 
    self.view.addSubview(mapHole) 
} 

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    if annotation is MKUserLocation { 
     return nil 
    } 

    let reuseId = "test" 

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) 
    if anView == nil { 
     anView = MKAnnotationView (annotation: annotation, reuseIdentifier: reuseId) 
     anView.image = UIImage(named:"ballSmall.png") 
     anView.canShowCallout = true 
    } else { 
     anView.annotation = annotation 
    } 

    return anView 
} 

誰能幫助?謝謝!我一直在研究和閱讀大約一週的時間,但我不明白。

回答

2

不會調用viewForAnnotation方法,因爲地圖視圖的delegate未以編程方式在viewDidLoad中創建時設置。

創建地圖視圖後,設置其屬性delegate

mapHole = MKMapView(frame: mapRect.textRect) 
mapHole.delegate = self      // <-- add this line 
span = MKCoordinateSpanMake(0.001, 0.001) 

此外,如果你還沒有,你必須聲明視圖控制器實現了MKMapViewDelegate協議。例如:

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 

注意,設置在地圖視圖中的delegate屬性必須完成以及宣佈該視圖控制器實現的協議。


你也應該從addStroke方法去除視圖相關的代碼,因爲它不屬於那裏,有沒有效果,是沒有意義的:

annotation.title = "Stroke" 

/* This code doesn't belong here... 
let useID = "test" 

var myAnnotation = mapHole.dequeueReusableAnnotationViewWithIdentifier(useID) 
if (myAnnotation == nil) { 
    myAnnotation = MKAnnotationView(annotation: annotation, reuseIdentifier: useID) 
    myAnnotation.image = UIImage(named: "ballSmall.png") 
    myAnnotation.canShowCallout = true 
    myAnnotation.enabled = true 
} else { 
    myAnnotation.annotation = annotation 
} 

mapHole.viewForAnnotation(annotation) 
*/ 

mapHole.addAnnotation(annotation) 


viewDidLoad還要注意以下代碼:

GPSspot = manager.location.coordinate 
region = MKCoordinateRegion(center: GPSspot, span: span) 
mapHole.setRegion(region, animated: true) 

有可能崩潰,因爲manager.location可能仍然nil在這一點所以小號在撥打startUpdatingLocation之後。首先檢查manager.location是不是nil或將該代碼移動到didUpdateLocations委託方法會更安全。


最後,關於:

...我添加的MKMapView編程到這樣我可以切換尺寸基於正在使用iPhone (水平和垂直)。 (我 仍然無法相信這不是直觀的Xcode和程序員 必須圍繞它進行編程。)

注意的Xcode僅僅是IDE,讓你編寫和編譯代碼。這是iOS SDK將知道哪些iPhone正在使用。 SDK可以根據屏幕大小自動調整視圖大小。在文檔,WWDC視頻,SO或其他資源中搜索「Auto Layout」和/或「Size Classes」。

+0

謝謝!!!!!我錯過了添加委託,現在它工作。謝謝! (現在要解決下個星期的長問題。) – Scuttle 2014-10-18 03:42:19