这是一个字符串,而我有一个地址数组。
var address = self.address
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [AnyObject]!, error: NSError!) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark {
self.mapView.addAnnotation(MKPlacemark(placemark: placemark))
}
})
答案 0 :(得分:1)
这样做。
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var mapView: MKMapView!
let addressArray = ["address1", "address2"]
let geocoder = CLGeocoder()
var count = 0
@IBAction func PlotAddresses(sender: AnyObject) {
plotAddressesInMap()
}
func plotAddressesInMap() {
if count < self.addressArray.count {
let address = addressArray[count]
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
print("Address = \(address)");
if let placemark = placemarks?.first {
self.mapView.addAnnotation(MKPlacemark(placemark: placemark))
}
print("Count = \(self.count)")
self.count += 1
self.plotAddressesInMap()
})
}
}
}