我正在尝试创建自定义标注,但由于某种原因,默认点图像不会更改。
我正在使用 Parse 获取点的数据。我创建了一个继承自 MKAnnotationView 的自定义类(customAnnotationView),并且我还覆盖了 mapView 函数。
视图控制器如下:
import UIKit
import Parse
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{
@IBOutlet weak var map: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate=self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
var annotationQuery = PFQuery(className: "Doc")
annotationQuery.findObjectsInBackgroundWithBlock {
(Doc, error) -> Void in
if error == nil {
// The find succeeded.
print("Successful query for annotations")
let docs = Doc
for doctor in docs! {
let point = doctor["location"] as! PFGeoPoint
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
annotation.title = String(doctor["name"])+String(doctor["surname"])
annotation.subtitle = String(doctor["address"])
print(doctor)
self.map.addAnnotation(annotation)
}
} else {
// Log details of the failure
print("Error: \(error)")
}
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
var userLocation:CLLocation = locations[0]
var userLangtitude = userLocation.coordinate.latitude
var userLongtitude = userLocation.coordinate.longitude
// Initialize Map
var latidude:CLLocationDegrees = userLangtitude
var longtitude:CLLocationDegrees = userLongtitude
var latDelta:CLLocationDegrees = 2
var lonDelta:CLLocationDegrees = 2
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latidude,longtitude)
var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
self.map.setRegion(region, animated: true)
self.map.showsUserLocation = true;
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// If annotation is user current location do not change the annotation
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("customAnnotationView")
if annotationView == nil{
annotationView = customAnnotationView(annotation: annotation, reuseIdentifier: "customAnnotationView")
annotationView?.canShowCallout = true
}
else{
annotationView?.annotation = annotation
}
return annotationView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
customAnnotationView:_
import UIKit
import MapKit
import CoreLocation
import Parse
class customAnnotationView: MKAnnotationView {
override init(frame: CGRect) {
super.init(frame: frame)
}
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
var frame = self.frame
frame.size = CGSizeMake(80,80)
self.frame = frame
self.backgroundColor = UIColor.clearColor()
self.centerOffset = CGPointMake(-5, -5)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(rect: CGRect) {
UIImage(named: "icon.png")?.drawInRect(CGRectMake(30,30, 30, 30))
}
}
任何帮助将不胜感激! 谢谢 !