8

有没有办法在 iOS 中获取一组 CLLocationsCoordinate2D 的中心坐标?

在此处输入图像描述

4

6 回答 6

14

嗯...这取决于您如何定义点的中心。是否取决于分布等...

一个简单的方法如下......

//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);

这将使矩形的中心覆盖所有点。但是,它没有考虑点差。

IE

. = point
X = centre

.....               X                   .

编辑

修正了一些数学。

于 2013-01-17T08:47:12.047 回答
6

我知道已经很晚了,但是有人可以阅读此内容并帮助自己使用 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;

问候!!!

于 2013-08-06T03:44:21.023 回答
5

您还可以同时获取s 和 acenter的数组。CLLocationCoordinate2Dspan

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
于 2017-12-07T12:30:29.500 回答
4

和 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));
}

}

于 2020-01-07T20:41:41.250 回答
1

如果您正在寻找作为中心的平均值,那么您将所有纬度和经度相加成它们自己的总数,然后除以您拥有的坐标数,这是非常标准的数学。

(请注意,如果您的坐标跨越日期线,这将不起作用)

于 2013-01-17T19:39:52.623 回答
1

Fogmeisters 的回答会起作用,但有 3 个错误,我已在下面更正:

//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);
于 2014-01-15T12:48:35.500 回答