如何从MKmapview的可见区域获取半径? [英] How to get radius from visible area of MKmapview?
问题描述
我能够获得地图视图的可见矩形,并且地图视图的中心点和跨度增量也可以通过mkmaap视图方法获得:
要变得可见,请使用:mapView.visibleMapRect
.获取中心点:使用map view.centerCoordinate
并获取跨度:使用mapView.region.span
.
I am able to get visible rectangle of map view and also centre point of map view and span deltas are also get from mkmaap view methods:
To get visible are :mapView.visibleMapRect
is used . To get centre point: map view.centerCoordinate
is used and to get span: mapView.region.span
is used.
现在我拥有所有信息,如何使用计算来计算可见半径?谁能详细解释一下我?
Now I have all the information, how can i calculate radius of visible are using calculation? Can anyone explain me in detail?
我已经看到这个问题但答案是我的跨度不是可见区域的半径.
I have seen this question but the answer is giving me span not radius of visible area.
推荐答案
使用 Swift 3.0 ,您可以使用扩展程序简化生活:
With Swift 3.0 you can use extension to simplify your life:
extension MKMapView {
func topCenterCoordinate() -> CLLocationCoordinate2D {
return self.convert(CGPoint(x: self.frame.size.width / 2.0, y: 0), toCoordinateFrom: self)
}
func currentRadius() -> Double {
let centerLocation = CLLocation(coordinate: self.centerCoordinate)
let topCenterCoordinate = self.topCenterCoordinate()
let topCenterLocation = CLLocation(coordinate: topCenterCoordinate)
return centerLocation.distance(from: topCenterLocation)
}
}
使用 Swift 4.0 :
extension MKMapView {
func topCenterCoordinate() -> CLLocationCoordinate2D {
return self.convert(CGPoint(x: self.frame.size.width / 2.0, y: 0), toCoordinateFrom: self)
}
func currentRadius() -> Double {
let centerLocation = CLLocation(latitude: self.centerCoordinate.latitude, longitude: self.centerCoordinate.longitude)
let topCenterCoordinate = self.topCenterCoordinate()
let topCenterLocation = CLLocation(latitude: topCenterCoordinate.latitude, longitude: topCenterCoordinate.longitude)
return centerLocation.distance(from: topCenterLocation)
}
}
这篇关于如何从MKmapview的可见区域获取半径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!