如何获取区域的坐标并将其设置为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
并将其设置为地图视图上的主要焦点区域?
答案 0 :(得分:0)
地标的region
属性类型为CLCircularRegion
(CLRegion
的子类),其中心坐标和半径以米为单位。
将其与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
}
}
})