搜索区域获取swift MKMapView中的坐标

时间:2014-11-11 13:10:13

标签: ios swift mkmapview clgeocoder

如何获取区域的坐标并将其设置为swift中mapview的焦点?

我有这段代码:

var address = "1 Steve Street, Johannesburg, South Africa"
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark {
    self.mapView.addAnnotation(MKPlacemark(placemark: placemark))
}
})

但是,从地址绘制一个点,我怎样才能得到一个区域,即:Johannesburg并将其设置为地图视图上的主要焦点区域?

1 个答案:

答案 0 :(得分:0)

地标的region属性类型为CLCircularRegionCLRegion的子类),其中心坐标和半径以米为单位。

将其与MKCoordinateRegionMakeWithDistance一起使用,您可以创建MKCoordinateRegion来设置地图视图' region

示例:

var address = "Johannesburg, South Africa"
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
    if let placemark = placemarks?[0] as? CLPlacemark {

        if let pmCircularRegion = placemark.region as? CLCircularRegion {

            let metersAcross = pmCircularRegion.radius * 2

            let region = MKCoordinateRegionMakeWithDistance
                  (pmCircularRegion.center, metersAcross, metersAcross)

            self.mapView.region = region
        }

    }
})