一组CLLocationsCoordinate2D的中心坐标

exe*_*ecv 5 objective-c coordinates mkmapview ios

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

在此输入图像描述

Fog*_*ter 12

嗯......这取决于你如何定义点的中心.它取决于分布等......

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

//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);
Run Code Online (Sandbox Code Playgroud)

这将使rect的中心覆盖所有点.但是,它没有考虑到传播.

. = point
X = centre

.....               X                   .
Run Code Online (Sandbox Code Playgroud)

编辑

纠正了一些数学.


小智 7

和 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));
}
Run Code Online (Sandbox Code Playgroud)

}


Hea*_*ets 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;
Run Code Online (Sandbox Code Playgroud)

问候!!!


Adr*_*ian 6

您还可以同时获取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)
  }
}
Run Code Online (Sandbox Code Playgroud)

用法:

let region = MKCoordinateRegion(coordinates: coordinates)
region.center
region.span
Run Code Online (Sandbox Code Playgroud)