top of page

How To Calculate Distance To Map Waypoint In Power Apps

How to calculate distance in Power Apps

In this article we will look at how we can calculate distance to a map waypoint in Power Apps. Out of the box, the Map control is one of the most versatile and complex controls to configure with plenty of customization.


The Map control allows us to configure destinations and waypoints, draw polygon shapes and calculate their perimeter and area and visualize routes.


I recently came across a number of ways to calculate distance, many becoming quite a costly solution by using third-party connectors. With a little trial and error, plus some assistance from AI, we can calculate the distance between our current device location and the location of a waypoint.


Getting The Device Location In Power Apps

Before we can begin to calculate distance we need to know the location of the users device in Power Apps. We can use the out of the box formulas to find this information which can be done using the following formulas:

Location.Latitude
Location.Longitude

Important: All users that are required to use this formula will initially be prompted to grant access to location services on mobile, tablet and desktop devices. It's worth mentioning that not all company devices or secure connections allow the location to be passed. Speak to your administrator should this be the case.


Set Up Waypoints Data In Power Apps

Waypoints or markers are configured by passing in the latitude and longitude of a location. We can do this by passing this data in from a data source or table. For this demonstration, I've done this using Named Formulas.


The following formula stores a table in a named formula called nfWaypoints. Each record in the table has an ID, which we can use should we need to identify each waypoint and the latitude and longitude of the waypoint.

nfWaypoints = 
    Table(
        {
            ID: 1,
            Latitude: 52.669947,
            Longitude: -2.442383
        },
        {
            ID: 2,
            Latitude: 52.672016,
            Longitude: -2.442168
        },
        {
            ID: 3,
            Latitude: 52.671313,
            Longitude: -2.439935
        }
	);


Calculate Distance Between Device & Waypoint in Power Apps

To calculate the distance between our device and the waypoint we can use the Haversine formula which is used to compute the distance between two points on the surface of a sphere.


We first need to know the radius of the earth, which is approximately 6,378km give or take a few kilometres dependant on where we measure it from. This will give us accurate enough data for most use cases. We can apply this to a new named formula:

nfEarthRadius = 6378;

The following formula takes the named formula nfWaypoints that we created earlier and cycles through each record applying the calculation to determine the distance from our current location, to the waypoint location.

nfWaypointsWithDistance = 
    ForAll(
        nfWaypoints,
        Patch(
            ThisRecord,
            { Distance: Round(
                nfEarthRadius * 2 * Asin(
                    Sqrt(
                        Power(Sin(Radians((Latitude - Location.Latitude) / 2)), 2) +
                        Cos(Radians(Location.Latitude)) *
                        Cos(Radians(Latitude)) *
                        Power(Sin(Radians((Longitude - Location.Longitude) / 2)), 2)
                    )
                ), 3)
            }
        )
    );

Show Distance On Map In Power Apps

Now we have all of the building blocks in place, let's implement our nfWaypointsWithDistance named formula on the map.


  • Add the Map control to the Canvas.

  • Apply the named formula to the Items property of the map.

Items = nfWaypointsWithDistance
  • Quickly visualize your distance value by adding the distance value to the fields property.


Add distance field to map control in power apps

  • Enable Show Info Cards (OnClick or OnHover).


Now, when you select on one of your waypoints on the map, you will see a pop out with the current distance.


Comments


bottom of page