更改MKMaps引脚颜色时出错

时间:2016-04-10 17:46:16

标签: ios swift mkmapview mapkit

我的代码绘制了一定数量的引脚。然后,用户可以在我的应用中绘制他们的位置。它应该是一种不同的颜色。我正在尝试这里的方法:Add different pin color with MapKit in swift 2.1。 但我一直收到错误说:无法将'NSKVONotifying_MKPointAnnotation'(0x7fc65c162c80)类型的值转换为'MyApp.ColorPointAnnotation'(0x104efcf00)。我的代码如下。

import UIKit
 import MapKit
import CoreLocation
 class WifiCode: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, NSXMLParserDelegate, UITextFieldDelegate {
var dictClients = [String:String]()
var arrayClients = NSMutableArray()

@IBOutlet weak var MapView: MKMapView!
@IBOutlet weak var Label1: UILabel!
@IBOutlet weak var textField2: UITextField!

@IBOutlet weak var Submit: UIButton!






func textFieldShouldReturn(textField2: UITextField) -> Bool {
    print("Okay")
   textField2.resignFirstResponder()
  return true
}
let LocationManager = CLLocationManager()
let initialLocation = CLLocation(latitude: 53.5, longitude: -8)
var regionRadius: CLLocationDistance = 1000

func centerMapOnLocation(location: CLLocation) {
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
        regionRadius * 150.0, regionRadius * 150.0)

    MapView.setRegion(coordinateRegion, animated: true)

}
override func viewDidLoad() {
    super.viewDidLoad()
   self.LocationManager.delegate = self
    self.MapView.delegate = self
    self.LocationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.LocationManager.requestWhenInUseAuthorization()
    self.LocationManager.startUpdatingLocation()
    centerMapOnLocation(initialLocation)
    self.textField2.delegate = self

   let path = NSBundle.mainBundle().pathForResource("wifi (1)", ofType: "txt")
    let filemgr = NSFileManager.defaultManager()

    if filemgr.fileExistsAtPath(path!){
        do{
            let fullText = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
            let readings = fullText.componentsSeparatedByString("\n")
            for i in 1..<readings.count{
                let data = readings[i].componentsSeparatedByString("\t") as [String]
                if data.count == 4{
                dictClients["name"] = "\(data[0])"
                dictClients["lat"] = "\(data[1])"
                dictClients["lng"] = "\(data[2])"
                dictClients["city"] = "\(data[3])"

            let london = MKPointAnnotation()
                   london.coordinate = CLLocationCoordinate2D(latitude: Double(data[2])!, longitude: Double(data[1])!)
                    london.title = data[0]
                    london.subtitle = data[3]
                    MapView.addAnnotation(london)
                }
            }
        }catch let error as NSError{

            print(error)

        }

    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



@IBAction func clicked(sender: AnyObject) {
    let address = textField2.text
    let geocoder = CLGeocoder()

    geocoder.geocodeAddressString(address!, completionHandler: {(placemarks, error) -> Void in
        if((error) != nil){
            print("Error", error)
        }
        if let placemark = placemarks?.first {
            let annotation = ColorPointAnnotation(pinColor: UIColor.blueColor())
            let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
            annotation.coordinate = coordinates
            annotation.title = "My Location"
            annotation.subtitle = ""
            self.MapView.addAnnotation(annotation)
             self.regionRadius = 250
            self.centerMapOnLocation(placemark.location!)

        }
    })

}



      /*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)

        let colorPointAnnotation = annotation as! ColorPointAnnotation
        pinView?.pinTintColor = colorPointAnnotation.pinColor
    }
    else {
        pinView?.annotation = annotation
    }

    return pinView
}
   }

0 个答案:

没有答案