[✅ Solution] Google maps link with direction from 2 address field

Hello Guys,

Not sure if anybody need this - trying to figure google direction link from 2 address fields

I made simple calculation field - see photos

The only problem is there was spaces in address calculation and whole filed doest recognize as link… only 1st part.

When you past whole calculation in new browser tab - directions works.

Is there a way to remove spaces in addresses calculation and replace with + sign… as that should work for link also?

Thank you


Hi Ivica!

You can try to add by the end of that formula this code:

.replaceAll(" ", "+")

If it doesnt work, you can try to refer this field to a new calculation:

@calculationfield.replaceAll(" ", "+")
3 Likes

Hi @ivica

Toni is not wrong in what he says and in fact, answers your question exactly. However, an alternative option based on what you are trying to achieve could be to encode the addresses.

const from = encodeURIComponent(@All of address[0]);
const to = encodeURIComponent(@All of Base[0]);
const baseUrl = 'https://www.google.com/maps/dir/';
const fLink = `${baseUrl}${from}/${to}`;
`
${fLink}
[Click here to get directions](${fLink})
`

This will give you:

  1. is the encoded web link represented in the code as ${fLink}
  2. is the long complex link hidden behind some words using markdown. which is the last line of the code above

I hope that makes sense

Jason

P.S: While it is not ideal if you put your URL with spaces into a markdown (as I have done in my example) or HTML link in the calculation block then the link will most likely work however as it is so easy in tape to encode the address I would recommend that, it also will deal with any other characters in the address that may not be liked in a link.

5 Likes

This is simple and works great… Thank you both Toni and Jason (as always) willing to help

1 Like

This opens good in same window. Any chance to open in new tab but also to work on mobile device ?

Jason,

Also … Can I get total distance in miles and display under the “Click here to get directions” with your method? Driving miles - Im assuming from google maps - driving option 1st.

@ivica

make an HTML link with a _blank target:

const from = encodeURIComponent(@All of address[0]);
const to = encodeURIComponent(@All of Base[0]);
const baseUrl = 'https://www.google.com/maps/dir/';
const fLink = `${baseUrl}${from}/${to}`;

`
<a href="${fLink}" target="_blank" rel="noopener noreferrer">Click here to get directions</a>
`

Distance you would need an API call, I show a Google distance here:

Then this one shows some more options:

The distance matrix in my screenshot is generated from calls to the Mapbox API and is more complex.

1 Like

Jason,

Coding machine… Happy 1year anniversary here … .We are glad to have you.

Im just wondering is there anything you dont know… I can try like this… :slight_smile:

What was temperature of the Sun in 1673 - May 14th at 13:04 CST at the depth of 1.34 miles… aha ? solve this one ? :slight_smile:

3 Likes

I want to just say Hot, very very hot! but the post has to be longer than that so I would say at a rough approximation something between 7 and 8 thousand degrees Celsius :wink:

1 Like