I am using the MvvmCross framework to build a cross platform (iOS, Android, WP7) application. I am using the MvxGeoLocationWatcher which creates MvxGeoLocation objects for me with the required Long, Lat, speed, heading etc.
I need to find the distance between two locations (as the crow flies). On Android I call fromLocation.distanceTo(toLocation). On iOs I would use CLLocationDistance distance = [fromLocation distanceFromLocation:toLocation];.
So both these platforms use a method on the location to calculate the distance and this isn’t currently available on the MvxGeoLocation object.
Are there plans to add it soon? If not what would be the best approach for me to add this functionality?
- Would I change the class itself and provide platform specific implementations – perhaps by making it a partial class and putting the specific code in a platform specific code partial class. Or perhaps by using extension methods?
- Or perhaps its best done as method that takes two points and returns the distance. In which case should this method be on MvxGeoLocationWatcher?
- Add the calculation algorithm in myself perhaps using something from Calculate distance of two geo points in km c# and forget about using the platform specific implementations
Currently mvvmcross has deliberately not included lat/lng calculations.
The motivation for this was:
It may be that mvvmcross provides an “official” location helper IoC plugin (or set of extension methods) at some point in the future… e.g. it maybe that we build an IoC plugin around http://xamarin.com/mobileapi … and I’m also very happy for others to build and publish them too.
So….
At a very simple level, working out the distance between two lat/lng points is easy to do in C# – there are heaps of fairly simple code example available like the excellent javascript ones at http://www.movable-type.co.uk/scripts/latlong.html – and there are heaps of libraries on github and codeplex like http://sharpmap.codeplex.com/
In RunSat (including on iPhone) I use:
Note that the license on this snippet is CC attribution – because its derived from movable type – http://www.movable-type.co.uk/scripts/latlong.html: