PowerAutomate – Transforming Lat/Long to an Address

Welcome to -33.796141, 151.178358! Wait, that doesn’t look right.

Hey everyone! Welcome to another tutorial from myself and the team at NextStep Creations. Today we’re using PowerAutomate to convert a latitude and longitude value to a readable address. In a previous blog we looked at Google Maps in PowerApps.

In this, we cover the ability to use a devices GPS coordinates to provide a location. But, if you were ever to display these, or print them or send them to a report (see Creating a HTML Report), they would come out as just a string of numbers, which isn’t ideal.

See Google Maps might know what to do with those numbers, but we don’t. So I’m going to show you how to convert those into something a bit more “human”.

Step 1 – Set up your Flow (PowerAutomate)

Make sure you have a Google Maps Developer account. See the Google Maps in PowerApps tutorial for info on how to do this. 

For this section, we’ll need to get an API Key for the Geocoding segment. Here’s a tutorial on how to do this: Get Geocoding API Key

We’re going to be working with PowerAutomate (Microsoft Flow) and PowerApps today.  Create a new flow, and name it “convertAddress”, Add the PowerApps trigger, and then a HTTP step after that:

PowerAutomate Setup

Perfect! Set the Method to “GET” and set the URI to the following code:

https://maps.googleapis.com/maps/api/geocode/json?latlng=@{triggerBody()[‘HTTP_URI’]}&key=YOUR API KEY HERE

Make sure to replace the key value with your API Key. You should now have a HTTP step that looks like this:

PowerAutomate - Transforming Lat/Long to an Address 1

Sweet! Let’s move on. In this next section, I’ve done a lot of the heavy lifting for you. In another blog post we’ll talk about how to correctly set up a Parse JSON step, but for now, here’s one I prepared earlier.

Add a Parse JSON step, and set the “Content” to the Body of the HTTP step previous.
In the schema, click this link and copy all of the text. Your Parse JSON step should look like so:

PowerAutomate - Transforming Lat/Long to an Address 2

Lets create a variable to store the address in. Add an Initialize Variable step, name it formattedAddress, set the type to String and the value to:

body(‘Parse_JSON’)?[‘results’][0][‘formatted_address’]
More on the above in the blog we’re writing about JSON. 
Now add a “Respond to a PowerApp or flow step, and set the output variable to the formattedAddress variable we made:
PowerAutomate - Transforming Lat/Long to an Address 3
 
Brilliant! Time to go to PowerApps.

Step 2 – Create PowerApps & tag the Flow

Alright, nice and easy. Create a new PowerApp, and add a button. We’re going to create a proof of concept app, to show that your latitude and longitude can be successfully converted to an address.

You can expand on this if you wish by adding the Google Maps ability like we’ve shown: Google Maps in PowerApps

Here’s my app layout:

PowerAutomate - Transforming Lat/Long to an Address 4

To get the text label to show my location, I’ve set the text property as follows:

“My location: ” & Location.Latitude & “, ” & Location.Longitude

This is using the in-built Location functionality in PowerApps.
Read more about that function here: Acceleration, App, Compass, Connection, and Location signals in Power Apps

Alright, let’s add some functionality! Add the flow to the button:

PowerAutomate - Transforming Lat/Long to an Address 5

In the OnSelect of the button, set a variable to the response value we defined (formatteddAddress) to a variable using the below code:

Set(convertedAddress, convertAddress.Run(Location.Latitude & “,” & Location.Longitude).formattedaddress)

PowerAutomate - Transforming Lat/Long to an Address 6

So, we’re just passing the latitude and longitude of the device, separated by a comma.

Now, set the bottom text label (“My address: “) to the following:

“My address:

” & convertedAddress

This will add a few blank lines, and then when you click the button will display your address.

Give it a try! You should see a result like so (I’ve blanked out some of my address for security reasons):

PowerAutomate - Transforming Lat/Long to an Address 7

And that’s it! Congratulations, you’ve got yourself an actual address.

Happy PowerApps developing, until next time.

 

Leave a Reply

Your email address will not be published. Required fields are marked *