iOS: How to convert MKMapPoint or CLLocationCoordinate2D to UTM?
-
26-09-2019 - |
Question
From what I have read this takes some complicated Math that I am not good at. So, I am asking here.
Does anyone have experience converting a MKMapPoint or CLLocationCoordinate2D to a UTM value? I found this resource (http://www.uwgb.edu/dutchs/usefuldata/UTMFormulas.HTM) but the math is overwhelming.
Solution
You could use one lib to do that, or analyze the code of one lib to understand the algorithm and do it yourself.
This is a c++ lib that does the job: http://geographiclib.sourceforge.net/html/
http://geographiclib.sourceforge.net/html/classGeographicLib_1_1UTMUPS.html
I found this website (http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html). If you look at the source code, the whole conversion is done using JavaScript, you can have a look at it and try to convert to Obj-c.
OTHER TIPS
I recently wrote a class for this and posted a sample project on GitHub
The part you want is a file called UTMConverter.m. It has methods for converting from lat/long to UTM and vice-versa.
MKMapViewZoom
appears to have some class methods which can convert between flat-map (geometric) & curved-map (geographic) coordinates, though I haven't tested them out. Somebody give me a thumbs up if this actually works
//convert from WGS84 (geographic coordinates) to UTM (geometric coordinates)
+ (double)longitudeToPixelSpaceX(double)pixelX
+ (double)latitudeToPixelSpaceY(double)pixelY
//convert from UTM to WGS84
+ (double)pixelSpaceXToLongitude(double)longitude
+ (double)pixelSpaceYToLatitude(double)latitude
some documentation here
- Objective C: https://github.com/jdp-global/MKMapViewZoom/blob/master/MKMapView%2BZoomLevel.m
- for RubyMotion: http://rubydoc.info/gems/map-kit-wrapper/0.0.5/frames
UPDATE:
This is maddening, but in order to get this class's source code to work accurately I basically had to extract the methods into my own domain essentially, then remove the parts of the code referencing MERCATOR_OFFSET
& change MERCATOR_RADIUS
to the meters value of the Earth's radius. I was kind of, ok, very surprised when I discovered this actually worked.