有没有办法在iOS中获取一组CLLocationsCoordinate2D的中心坐标?
有没有办法在iOS中获取一组CLLocationsCoordinate2D的中心坐标?
嗯...这取决于你如何定义点的中心。它是否取决于分布等等...
一种简单的方法是按以下方式进行...
//find rect that encloses all coords
float maxLat = -200;
float maxLong = -200;
float minLat = MAXFLOAT;
float minLong = MAXFLOAT;
for (int i=0 ; i<[locations count] ; i++) {
CLLocationCoordinate2D location = [locations objectAtIndex:i];
if (location.latitude < minLat) {
minLat = location.latitude;
}
if (location.longitude < minLong) {
minLong = location.longitude;
}
if (location.latitude > maxLat) {
maxLat = location.latitude;
}
if (location.longitude > maxLong) {
maxLong = location.longitude;
}
}
//Center point
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((maxLat + minLat) * 0.5, (maxLong + minLong) * 0.5);
这将给出覆盖所有点的矩形的中心。然而,它没有考虑散布。
即
. = point
X = centre
..... X .
编辑
修正了部分数学内容。
同时兼容Swift 5的扩展
extension Array where Element == CLLocationCoordinate2D {
func center() -> CLLocationCoordinate2D {
var maxLatitude: Double = -200;
var maxLongitude: Double = -200;
var minLatitude: Double = Double(MAXFLOAT);
var minLongitude: Double = Double(MAXFLOAT);
for location in self {
if location.latitude < minLatitude {
minLatitude = location.latitude;
}
if location.longitude < minLongitude {
minLongitude = location.longitude;
}
if location.latitude > maxLatitude {
maxLatitude = location.latitude;
}
if location.longitude > maxLongitude {
maxLongitude = location.longitude;
}
}
return CLLocationCoordinate2DMake(CLLocationDegrees((maxLatitude + minLatitude) * 0.5), CLLocationDegrees((maxLongitude + minLongitude) * 0.5));
}
}
CLLocationCoordinate2D
数组的中心和span
。extension MKCoordinateRegion {
init(coordinates: [CLLocationCoordinate2D]) {
var minLatitude: CLLocationDegrees = 90.0
var maxLatitude: CLLocationDegrees = -90.0
var minLongitude: CLLocationDegrees = 180.0
var maxLongitude: CLLocationDegrees = -180.0
for coordinate in coordinates {
let lat = Double(coordinate.latitude)
let long = Double(coordinate.longitude)
if lat < minLatitude {
minLatitude = lat
}
if long < minLongitude {
minLongitude = long
}
if lat > maxLatitude {
maxLatitude = lat
}
if long > maxLongitude {
maxLongitude = long
}
}
let span = MKCoordinateSpanMake(maxLatitude - minLatitude, maxLongitude - minLongitude)
let center = CLLocationCoordinate2DMake((maxLatitude - span.latitudeDelta / 2), (maxLongitude - span.longitudeDelta / 2))
self.init(center: center, span: span)
}
}
使用方法:
let region = MKCoordinateRegion(coordinates: coordinates)
region.center
region.span
我知道现在已经很晚了,但是如果有人看到这条信息并需要帮助的话,可以参考Fogmeister的回答,创建一个MKCoordinateRegion。
CLLocationDegrees minLat,minLng,maxLat,maxLng;
for(CLLocationCoordinate2D coordinate in coordinates) {
minLat = MIN(minLat, coordinate.latitude);
minLng = MIN(minLng, coordinate.longitude);
maxLat = MAX(maxLat, coordinate.latitude);
maxLng = MAX(maxLng, coordinate.longitude);
}
CLLocationCoordinate2D coordinateOrigin = CLLocationCoordinate2DMake(minLat, minLng);
CLLocationCoordinate2D coordinateMax = CLLocationCoordinate2DMake(maxLat, maxLng);
MKMapPoint upperLeft = MKMapPointForCoordinate(coordinateOrigin);
MKMapPoint lowerRight = MKMapPointForCoordinate(coordinateMax);
//Create the map rect
MKMapRect mapRect = MKMapRectMake(upperLeft.x,
upperLeft.y,
lowerRight.x - upperLeft.x,
lowerRight.y - upperLeft.y);
//Create the region
MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect);
//THIS HAS THE CENTER, it should include spread
CLLocationCoordinate2D centerCoordinate = region.center;
您好!
一个小型的数组扩展,没有像-200.0
这样的神奇数字。
extension Array where Element == CLLocationCoordinate2D {
func center() -> CLLocationCoordinate2D {
var maxLatitude: Double = -.greatestFiniteMagnitude
var maxLongitude: Double = -.greatestFiniteMagnitude
var minLatitude: Double = .greatestFiniteMagnitude
var minLongitude: Double = .greatestFiniteMagnitude
for location in self {
maxLatitude = Swift.max(maxLatitude, location.latitude)
maxLongitude = Swift.max(maxLongitude, location.longitude)
minLatitude = Swift.min(minLatitude, location.latitude)
minLongitude = Swift.min(minLongitude, location.longitude)
}
let centerLatitude = CLLocationDegrees((maxLatitude + minLatitude) * 0.5)
let centerLongitude = CLLocationDegrees((maxLongitude + minLongitude) * 0.5)
return .init(latitude: centerLatitude, longitude: centerLongitude)
}
}
//find rect that encloses all coords
float maxLat = -200;
float maxLong = -200;
float minLat = MAXFLOAT;
float minLong = MAXFLOAT;
for (int i=0 ; i<[locations count] ; i++) {
CLLocationCoordinate2D location = [locations objectAtIndex:i];
if (location.latitude < minLat) {
minLat = location.latitude;
}
if (location.longitude < minLong) {
minLong = location.longitude;
}
if (location.latitude > maxLat) {
maxLat = location.latitude;
}
if (location.longitude > maxLong) { //Change to be greater than
maxLong = location.longitude;
}
}
//Center point
//The min's and max's should be ADDED not subtracted
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((maxLat + minLat) * 0.5, (maxLong + minLong) * 0.5);
如果您想以平均值作为中心,则将所有纬度和经度分别加总,然后除以您拥有的坐标数量,这是相当标准的数学方法。
(请注意,如果您的坐标跨越日期线,则此方法不适用)