我正在尝试在应用中实现从源到目标的地图导航。我只是想根据中心坐标动态计算latitudedelta和longitudedelta。
let midLat = (self.source.coordinate.latitude + self.destination.coordinate.latitude)/2
let midLang = (self.source.coordinate.longitude + self.destination.coordinate.longitude)/2
let centerCoordinate = CLLocationCoordinate2DMake(midLat, midLang)
self.mapView.setRegion(MKCoordinateRegionMake(centerCoOrdinate, MKCoordinateSpanMake(0.09, 0.09)), animated: true)
答案 0 :(得分:0)
首先,您在计算中间位置时遇到问题,您必须使用此处建议的扩展程序检查此答案Determining Midpoint Between 2 Cooridinates
import Foundation
import MapKit
extension CLLocationCoordinate2D {
// MARK: CLLocationCoordinate2D+MidPoint
func middleLocationWith(location:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
let lon1 = longitude * M_PI / 180
let lon2 = location.longitude * M_PI / 180
let lat1 = latitude * M_PI / 180
let lat2 = location.latitude * M_PI / 180
let dLon = lon2 - lon1
let x = cos(lat2) * cos(dLon)
let y = cos(lat2) * sin(dLon)
let lat3 = atan2( sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y) )
let lon3 = lon1 + atan2(y, cos(lat1) + x)
let center:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat3 * 180 / M_PI, lon3 * 180 / M_PI)
return center
}
}
然后你就像这样使用它
let middleLocation = self.source.coordinate.middleLocationWith(location: self.destination.coordinate)
self.mapView.setRegion(MKCoordinateRegionMake(middleLocation, MKCoordinateSpanMake(0.09, 0.09)), animated: true)
我希望这有助于你