让注释出现在地图上时遇到了一些麻烦。地址字符串是从前一个 ViewController 传入的。一切正常,直到将注释推送到地图。有什么建议么?
class PostViewController: UIViewController {
@IBOutlet weak var locationInput: UITextField!
@IBOutlet weak var debugText: UILabel!
@IBAction func postDirectionTouched(sender: AnyObject) {
if locationInput.text!.isEmpty {
debugText.text = "Location Input Empty."
} else {
nextSegue()
}
}
func nextSegue() {
dispatch_async(dispatch_get_main_queue(), {
let controller = self.storyboard!.instantiateViewControllerWithIdentifier("LinkPostController") as! LinkPostViewController
controller.address = self.locationInput.text!
self.presentViewController(controller, animated: true, completion: nil)
})
}
}
import UIKit
import CoreLocation
import AddressBook
import MapKit
class LinkPostViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var pointMap: MKMapView!
@IBOutlet weak var linkInput: UITextField!
@IBOutlet weak var debugText: UILabel!
var address: String = ""
var coords: CLLocationCoordinate2D?
override func viewDidLoad() {
super.viewDidLoad()
self.mapLocation()
}
func mapLocation(){
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(self.address, completionHandler: {(placemark, error) -> Void in
if error != nil{
print("Geocode failed with error: \(error!.localizedDescription)")
} else if placemark!.count > 0 {
let pm = placemark?[0]
let location = pm!.location
self.coords = location!.coordinate
print(self.coords!)
self.showMap()
}
})
}
func showMap() {
print("last")
let coordinates = self.coords
let pinn = MKPointAnnotation()
pinn.title = "hello"
pinn.subtitle = "This my pin."
pinn.coordinate = coordinates!
dispatch_async(dispatch_get_main_queue()) {
self.pointMap.addAnnotation(pinn)
}
}
func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {}
}