top of page

Calculate distances between geographic coordinates with latitude and longitude

Updated: Feb 1, 2023

I was developing an app that used the mapping control within canvas apps and needed to find a method to calculate the distance between two geographic coordinates. This post describes my approach and can be used for any situation where latitude and longitude coordinates are used.


Introduction


In order to use the map, geospatial services must be enabled in the environment you are using. Here are the instructions to follow to make this is enabled.


Microsoft Dataverse and Spatial Services connectors need to be classified under the same Data Loss Prevention (DLP) data group such as 'Business'. Please note that the map is a premium feature within canvas apps at the time of writing.



Current location


Power Apps can determine the current location of the device based on the GPS and other device information, such as IP address. Note that your device or browser may ask you to allow access to your location.


Location.Latitude - Returns a number, from –90 to 90, that indicates the latitude, as measured in degrees from the equator. A positive number indicates a location that's north of the equator.


Location.Longitude - Returns a number, from –180 to 180, that indicates the longitude, as measured in degrees from Greenwich, England. A positive number indicates a location that's east of Greenwhich.



Location data


For this example, I collected the geographic coordinates and name of some famous locations in the United Kingdom. I used the ClearCollect command and named the collection as colLocations.


ClearCollect(
    colLocations,
    Table(
        {
            Name: "Buckingham Palace",
            Latitude: 51.501476,
            Longitude: -0.140634
        },
        {
            Name: "Winchester Cathedral",
            Latitude: 51.060650,
            Longitude: -1.312791
        },
        {
            Name: "Scottish National Portrait Gallery",
            Latitude: 55.955418,
            Longitude: -3.193583
        },
        {
            Name: "Snowdonia National Park",
            Latitude: 52.918011,
            Longitude: -3.891220
        },
        {
            Name: "Silverstone Circuit",
            Latitude: 52.073273,
            Longitude: -1.014818
        },
        {
            Name: "The Giant's Causeway",
            Latitude: 55.240833,
            Longitude: -6.511667
        }
    )
)

I connected my map to the colLocations collection and set the current location latitude to Location.Latitude and current location longitude to Location.Longitude.


Set the advanced setting ItemsLatitudes to "Latitude" and the ItemsLongitudes to "Longitude".



You should now see pins on the map and your current location. You can edit the style of the map including the pin colour. The Map style I selected for this example was 'Grayscale light'.


Calculating distances


Introducing the haversine formula...! This magical formula determines the great-circle distance between two points on a sphere given their longitudes and latitudes. It dates back to the early 1800s and is used in navigation.


There is lots of background reading on haversine if you're interested, but I have simplified the formula for use in Power Fx as follows:


Acos(Sin(Pi() * Value(lblMyLatitude.Text) / 180.0) * Sin(Pi() * Latitude / 180.0) + Cos(Pi() * Value(lblMyLatitude.Text) / 180.0) * Cos(Pi() * Latitude / 180.0) * Cos(Pi() * Value(lblMyLongitude.Text) / 180.0 - Pi() * Longitude /180.0)) * 6378

It looks complicated, but the only variables that change are lblMyLatitude.Text and lblMyLongitude.Text which are labels containing Location.Latitude and Location.Longitude respectively. Latitude and Longitude in the formula are referencing columns in colLocations.


My method to show the distances clearly to the user required a gallery with the AddColumns command displaying the distances calculated ("DistanceKM") from the haversine formula. The gallery could then be sorted by 'DistanceKM' to display the nearest location at the top.



This is the full code snippet I used for the gallery:


SortByColumns(
    AddColumns(
        colLocations,
        "DistanceKM",
        Round(
            Acos(Sin(Pi() * Value(lblMyLatitude.Text) / 180.0) * Sin(Pi() * Latitude / 180.0) + Cos(Pi() * Value(lblMyLatitude.Text) / 180.0) * Cos(Pi() * Latitude / 180.0) * Cos(Pi() * Value(lblMyLongitude.Text) / 180.0 - Pi() * Longitude /180.0)) * 6378,
            1
        )
    ),
    "DistanceKM",
    Ascending
)

Here is some further detail on the elements I used within the gallery. For tips on formatting the gallery, check out my previous blog post on transforming canvas app galleries using a few basic steps.




Conclusion


The haversine formula is extremely useful for calculating distances between two geographic coordinates. This post gives a simplified formula that can be used in Power Fx and implemented within a sorted gallery to list ordered locations based on your current position.


There is obviously so much scope to expand this for multiple use cases once you have the distances calculated.


I hope this post was useful and helps you with your tasks involving calculated distances in Power Fx.



bottom of page