如何在 ios Swift 中创建类似于 50 英里半径的 MKCoordinate 区域?

How to create an MKCoordinate region similar to 50 miles radius in ios Swift?

我正在尝试使用 CLLocation 创建一个类似于圆半径的区域。我了解半径逻辑及其以米为单位的测量方式,但在 MKCoordinate 区域以及 delta 和 lat delta 转换为面积时不太清楚。我想要一个 75 英里的区域。这是我的代码....

let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

如果您能提供一个解释,而不仅仅是一个简短的回答,我们将不胜感激。

您可以使用 MKCoordinateRegionMakeWithDistance 函数:

Creates a new MKCoordinateRegion from the specified coordinate and distance values.

func MKCoordinateRegionMakeWithDistance(
_ centerCoordinate: CLLocationCoordinate2D, 
_ latitudinalMeters: CLLocationDistance, 
_ longitudinalMeters: CLLocationDistance) -> MKCoordinateRegion

centerCoordinate - The center point of the new coordinate region.

latitudinalMeters - The amount of north-to-south distance (measured in meters) to use for the span.

longitudinalMeters - The amount of east-to-west distance (measured in meters) to use for the span.

所以你会有这样的东西:

let rect = MKCoordinateRegionMakeWithDistance(center, 50 * 1609.34, 50 * 1609.34)

如果您要创建一个实际的圆形区域:

let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let radius: CLLocationDistance = 60350.4    // meters for 37.5 miles
let regionIdentifier = "CircularRegion"     // any desired String

let circularRegion = CLCircularRegion(center: center, radius: radius, identifier: regionIdentifier)