DEV Community

Pietro Bongiovanni
Pietro Bongiovanni

Posted on

A quick guide to creating Google Maps URLs for your applications


Having to find directions to an address that is in an app can be quite painful. Not all apps allow copy pasting the text, maybe it’s not in text format but has been added to the image and nobody ever thought about making it a simple selectable screen, there are many reasons why this could be hard.
So it’s us, when developing our applications and websites, that have to think about the users that might find themselves in the same situations as we are, trying to figure out the direction to that freaking coffee shop that everybody talks about but whose website seems to be a static image that does not allow you to select a thing.
And the solution is very easy: just make sure that under your address string you place an href tag with a Google Maps URL! It’s easier than you think and it might solve all your users problems.
How to do that? Well simply take the address you want the users to reach and simply create a link in the following form:
'https://www.google.com/maps/dir/?api=1&destination=' . $address
What this URL will do is simply redirect the user to a Google Maps page showing directions from their current location to the address stored in the variable $address or, if they are on mobile, launch the Google Maps app which will then navigate their user from their current location to the address you specified.
Note that you are not limited to simply the address! You can use almost every query you can perform on Google Maps as destination value: make it Latitude and Longitude, postcode or whatever other value you prefer, and your users will be able to reach their destination without having to struggle with copying and pasting addresses!

P.S. Remember to encode (urlencode()) the address before appending it to the Google Maps api base URL!

P.P.S. Concatenation and variable syntax here is PHP but the concept is valid for every programming language ;)

Oldest comments (0)