Finding the distance between two points using Android MapView

In developing an application that is soon to be released in the Norwegian marketplace I’ve been struggling a bit with finding the distance between two points so I can use the MapController’s zoomToSpan method that zooms to fit a given span/region.

The solution that was presented to me was using Location.distanceBetween(latA, lonA, latB, lonB); but little did I know that I would spend hours of tracking down why this method wouldn’t work for me.

The solution? We already know (at least by now) that when you’re given coordinates in the form of “Lat: 51.45999, Lon: 10.344” you need to extract those values and multiply it by 1E6 (10^6) to get a valid GeoPoint in micro-degrees. The problem I was having was that I passed on to Location.distanceBetween those values instead of the original values which caused some head-scratching moments and lots of frustration.

In order to use Location.distanceBetween(); correctly I had to divide my GeoPoint locations by 1E6 in order to get a valid calculation.

So if you’re struggling with distanceBetween and zoomToSpan make sure you’re working with valid data!