Search code examples
phpgoogle-mapsqr-code

How to include %20 spaces in Google QR code URL after scanning?


I'm trying to generate QR code images that contain Google Maps routes. For example, I have the following Google Maps URL:

$encodedmapsurl = https://www.google.nl/maps/dir/De%20Aar%2018,%208253%20PN%20Dronten/De%20Lepelaar%2012,%208251MT%20Dronten/De%20Lepelaar%201,%208251mt%20Dronten/De%20Aar%2018,%208253%20PN%20Dronten/

And I generate the Google Charts QR code like this:

<img src="https://api.qrserver.com/v1/create-qr-code/?data=$encodedmapsurl&amp;size=150x150" />

I end up with the following QR code:

enter image description here

This is fine, however, when I scan the QR code with a mobile application, the result is:

enter image description here

Notice that the URL itself contains %20 for every space in the URL, but the result (after scanning the QR code) does not. This results in an unclickable URL. This behaviour is weird considering some QR code generators do include the %20 in the URL after scanning, such as this one. Is there any way with the Google Charts QR code API to include the %20's in the URL after scanning it? Alternatively I'm also fine with using another PHP library/API that does the job (I couldn't find one). The result should look like this:

enter image description here


Solution

  • Try formally encoding it as a url first:

    $encodedmapsurl = "https://www.google.nl/maps/dir/De%20Aar%2018,%208253%20PN%20Dronten/De%20Lepelaar%2012,%208251MT%20Dronten/De%20Lepelaar%201,%208251mt%20Dronten/De%20Aar%2018,%208253%20PN%20Dronten/";
    $encodedmapsurl = urlencode($encodedmapsurl);
    

    Also, then try also then sending the encoded url to bitly or similar shortener before generating the qr code.

    If that still doesn't work, being in Japan I often find special characters leading to many hard-to-find/fix issues (e.g. a 1-bit "%" vs a 2-bit "%" character). Try using the charset-source (and charset-target) parameter.