Search code examples
iosswiftcllocationmanagercllocation

How can I get a grid reference from my CLLoocation.coordinate


My app is sending location coordinates to a server by getting the longitude and latitude using the code below.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    let locationArray = locations as NSArray
    let locationObj = locationArray.lastObject as! CLLocation
    let coord = locationObj.coordinate

    latitudeTxt = String(format: "%.4f", coord.latitude)
    longitudeTxt = String(format: "%.4f",coord.longitude)
}

I've now been asked to send the values as a grid reference and the only way I have found to do this involves very complex algorithms.

Is there any easier way to get a grid reference from my longitude and latitude values?


Solution

  • There's no native support, but I did find this project on github: GeodeticUTMConverter. It seems to have implemented the algorithms used to convert the lat/long to easting & northings, you may have a bit of work to get an exact grid reference but it'll be a good starting point.