gpt4 book ai didi

ios - 将MapKit经纬度转为DMS格式

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:00 26 4
gpt4 key购买 nike

我可以通过以下方式确定 map 中心位置的纬度和经度:

func TargetGridReference(outletMapView_PrimaryTargetLocationMap: MKMapView, regionDidChangeAnimated animated: Bool) {

let ThreatGridReference = outletMapView_PrimaryTargetLocationMap.centerCoordinate

let mapLatitude = String(format:"%.13f",ThreatGridReference.latitude)

let mapLongitude = String(format:"%.13f",ThreatGridReference.longitude)
let latAndLong = "Lat: \(mapLatitude) \nLong: \(mapLongitude)"

self.outletLabel_TargetGridReference.text = latAndLong

tempThreatGridReference = ThreatGridReference
tempThreatGridReferenceLat = mapLatitude
tempThreatGridReferenceLon = mapLongitude

}

这给了我巴黎艾菲尔铁塔的纬度:

Lat:  48.8582487759147
Long: 2.2945180844931

如何在 Swift 中将以上内容转换为以下 DMS 格式?:

lat: 48° 51' 29.6956" N
long: 2° 17' 40.2651" E

MapKit 是否提供自动转换为各种坐标系(如 UTM、UPS、MGRS 等)的功能?

最佳答案

遗憾的是没有自动转换,但您可以按如下方式轻松创建一个:

Swift 4 或更高版本

func coordinateToDMS(latitude: Double, longitude: Double) -> (latitude: String, longitude: String) {
let latDegrees = abs(Int(latitude))
let latMinutes = abs(Int((latitude * 3600).truncatingRemainder(dividingBy: 3600) / 60))
let latSeconds = Double(abs((latitude * 3600).truncatingRemainder(dividingBy: 3600).truncatingRemainder(dividingBy: 60)))

let lonDegrees = abs(Int(longitude))
let lonMinutes = abs(Int((longitude * 3600).truncatingRemainder(dividingBy: 3600) / 60))
let lonSeconds = Double(abs((longitude * 3600).truncatingRemainder(dividingBy: 3600).truncatingRemainder(dividingBy: 60) ))

return (String(format:"%d° %d' %.4f\" %@", latDegrees, latMinutes, latSeconds, latitude >= 0 ? "N" : "S"),
String(format:"%d° %d' %.4f\" %@", lonDegrees, lonMinutes, lonSeconds, longitude >= 0 ? "E" : "W"))
}

let dms = coordinateToDMS(latitude: 48.8582487759147, longitude: 2.2945180844931)

print(dms.latitude) // "48° 51' 29.6956" N"
print(dms.longitude) // "2° 17' 40.2651" E"

您还可以扩展 CLLocationCoordinate2D 以获取您的纬度/经度 DMS 描述:

import MapKit
extension FloatingPoint {
var minutes: Self {
return (self*3600)
.truncatingRemainder(dividingBy: 3600)/60
}
var seconds: Self {
return (self*3600)
.truncatingRemainder(dividingBy: 3600)
.truncatingRemainder(dividingBy: 60)
}
}
extension CLLocationCoordinate2D {
var dms: (latitude: String, longitude: String) {
return (String(format:"%d° %d' %.4f\" %@",
Int(abs(latitude)),
Int(abs(latitude.minutes)),
abs(latitude.seconds),
latitude >= 0 ? "N" : "S"),
String(format:"%d° %d' %.4f\" %@",
Int(abs(longitude)),
Int(abs(longitude.minutes)),
abs(longitude.seconds),
longitude >= 0 ? "E" : "W"))
}
}

let coord = CLLocationCoordinate2D(latitude: 48.8582487759147, longitude: 2.2945180844931)

coord.dms.latitude // "48° 51' 29.6956" N"
coord.dms.longitude // "2° 17' 40.2651" E"

关于ios - 将MapKit经纬度转为DMS格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35120793/

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