gpt4 book ai didi

ios - 计算 CLLocation 数组的中心坐标点

转载 作者:行者123 更新时间:2023-11-29 00:33:40 25 4
gpt4 key购买 nike

如果我们有多个 CLLocation 点,如何计算 MapView 的中心点?

最佳答案

func zoomToFitPolyLine(pointsArr : NSArray) {
if (pointsArr.count == 0) {
return
}
var topLeftCoord : CLLocationCoordinate2D? = CLLocationCoordinate2DMake(-90, 180 )

var bottomRightCoord : CLLocationCoordinate2D? = CLLocationCoordinate2DMake(90, -180 )

for (index,_) in (pointsArr.enumerated()) {

let tempLoc : CLLocation? = pointsArr.object(at: index) as? CLLocation

topLeftCoord?.latitude = fmax((topLeftCoord?.latitude)!, (tempLoc?.coordinate.latitude)!)
topLeftCoord?.longitude = fmin((topLeftCoord?.longitude)!, (tempLoc?.coordinate.longitude)!)

bottomRightCoord?.latitude = fmin((bottomRightCoord?.latitude)!, (tempLoc?.coordinate.latitude)!)
bottomRightCoord?.longitude = fmax((bottomRightCoord?.longitude)!, (tempLoc?.coordinate.longitude)!)
}

var centerCoordinate : CLLocationCoordinate2D? = CLLocationCoordinate2DMake(0, 0)

centerCoordinate?.latitude = (topLeftCoord?.latitude)! - ((topLeftCoord?.latitude)! - (bottomRightCoord?.latitude)!) * 0.5;
centerCoordinate?.longitude = (topLeftCoord?.longitude)! + ((bottomRightCoord?.longitude)! - (topLeftCoord?.longitude)!) * 0.5;
self.zoomToRegion(location: centerCoordinate!)
}

func zoomToRegion(location:CLLocationCoordinate2D)
{
let camera : MKMapCamera = MKMapCamera.init(lookingAtCenter: location, fromEyeCoordinate: location, eyeAltitude: CLLocationDegrees(10));
myMap?.setCamera(camera, animated: true);
}

关于ios - 计算 CLLocation 数组的中心坐标点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com