如何在MapView上绘制字符串数组地址并显示标记?

时间:2016-06-27 03:26:44

标签: ios swift google-maps mkmapview

这是一个字符串,而我有一个地址数组。

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))
            }
        })

1 个答案:

答案 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()
            })
        }
    }
}