将MapKit纬度和经度转换为DMS格式

将MapKit纬度和经度转换为DMS格式

本文介绍了将MapKit纬度和经度转换为DMS格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下内容确定地图中心位置的纬度和经度:

I am able to determine the latitude and longitude of a location in the center of a map with the following:

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

}

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

This gives me the latitude for the Eiffel Tower in Paris as:

Lat:  48.8582487759147
Long:  2.2945180844931

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

How can I convert the above to the following DMS format in Swift?:

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

MapKit能否自动转换为各种坐标系统,如UTM,UPS,MGRS等?

Does MapKit provide automatic conversion into the various coordinate systems like UTM, UPS, MGRS, etc?

推荐答案

不幸的是,没有自动转换,但你可以轻松创建一个如下:

Unfortunately there is no automatic conversion but you can easily create one as follow:

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描述:

You can also extend CLLocationCoordinate2D to get your latitude/longitude DMS description:

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"

这篇关于将MapKit纬度和经度转换为DMS格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:22