Home > Mobile >  Making a hyperlink from a address
Making a hyperlink from a address

Time:10-14

So i made a simple product ordering website. When a customer wants to order a product he enters his details name, phone number and address. And on the admin panel i receive all the details in clear text.

So i have a question, not sure if this is possible at all i am just a newbie. I want that address part to convert from clear text to a hyperlink (on admin panel) which will lead to google maps website/app so that i can just press "Directions" after that and deliver it to the customer. I don't always want to do it manualy (copy paste).

example:

| Tommy Vercetti | Hawaii Shirt | 123 456789 | Vice City | Ocean Drive 123 |

So when i press the "Ocean Drive 123" part i just want it to open the google maps app/website for me automatically.

CodePudding user response:

I assume the full address is: Ocean View 123, Hilo, HI, USA

<a href="https://www.google.ca/maps/place/Ocean View 123, Hilo, HI, USA>Ocean View 123</a>

just add in places of all white spaces and it should work. Ocean View 123

CodePudding user response:

You need to encode the data using rawurlencode().

<?php
$customeraddress = "Ocean Drive 123, Vice City";
$businessaddress = "123 business address lane, Vice City";

echo '<a href='https://www.google.ca/maps/dir/',rawurlencode($businessaddress),'/',rawurlencode($customeraddress), '">';
?>

CodePudding user response:

There are some advanced techniques to build up a valid Google Maps URL, but the easiest one I can think of is construct a simple URL that concatenates your address to a predefined URL.

Notes

http://sandbox.onlinephpfunctions.com/code/9f584e44e1e9ca65f65f59fe70a1f41b4c416446

<?php

// The address you're starting from.
$origin = '125 Union St, Brooklyn, NY';

// The destination address you're going to. 
$destination = '500 King St S., Toronto';

// Google Maps URL which is made up of origin and destination. URL params are encoded. 
$url = sprintf('https://www.google.ca/maps/dir/%s/%s', urlencode($origin), urlencode($destination));

// Use target="_blank" so the link opens in a new tab. 
echo sprintf('<a href="%s" target="_blank">%s</a>', $url, $destination);

// Produces this URL
<a href="https://www.google.ca/maps/dir/125 Union St, Brooklyn, NY/500 King St S., Toronto" target="_blank">500 King St S., Toronto</a>
  • Related