swiftgpsgisprojepsg

Swift - Coordinate Transformation from EPSG:4326 to 3857


I would like to know a Swift library or method that I can use to convert from 4326 to 3857. I have referred some libraries such as Proj4Swift and Proj.4, but to no avail. The first link does not has a workable download and the second does not has a documentation. Would anyone suggest a library or method that I can use to achieve this in Swift?

I have also referred other similar questions but could not find a proper solution.


Solution

  • Well, since I couldn't find a clear and direct answer, I wrote the code in Swift, by referring the following and this. It's as follows.

    func getCoordinatesInEPSG3857(longitudeInEPSG4326: Double, latitudeInEPSG4326: Double) -> (Double, Double) {
        let longitudeInEPSG3857 = (longitudeInEPSG4326 * 20037508.34 / 180)
        let latitudeInEPSG3857 = (log(tan((90 + latitudeInEPSG4326) * Double.pi / 360)) / (Double.pi / 180)) * (20037508.34 / 180)
    
        return (longitudeInEPSG3857, latitudeInEPSG3857)
    }
    

    Here, you can pass the coordinates which are in EPSG:4326 and the function returns the coordinates in EPSG:3857.