我不明白如何更改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
}
誰能幫助?謝謝!我一直在研究和閱讀大約一週的時間,但我不明白。
謝謝!!!!!我錯過了添加委託,現在它工作。謝謝! (現在要解決下個星期的長問題。) – Scuttle 2014-10-18 03:42:19