Last active
March 20, 2025 13:51
-
-
Save jeteon/e71fa21c1feb48fe4b5eeec045229a0c to your computer and use it in GitHub Desktop.
NodeJS script to parse the Google Maps "data" URL attribute into an array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* Basic code to parse the values in the "data" attribute in a Google Maps URL to an Array. | |
* There will likely still be some work to do to interpret the resulting Array. | |
* | |
* Based on information from: | |
* http://stackoverflow.com/a/34275131/1852838 | |
* http://stackoverflow.com/a/24662610/1852838 | |
*/ | |
const util = require('util'); | |
// Data string to be parsed | |
var str = '!3m1!4b1!4m20!4m19!1m5!1m1!1s0x1e955fe737bd22e5:0xf5b813675e007ba8!2m2!1d28.3023579!2d-25.7297871!1m5!1m1!1s0x1e955e5c875906fd:0xa65176214cdebc80!2m2!1d28.3374283!2d-25.7657075!1m5!1m1!1s0x1e9560c06dba5b73:0x57122f60632be1a1!2m2!1d28.274954!2d-25.7832822!3e0'; | |
var parts = str.split('!').filter(function(s) { return s.length > 0; }), | |
root = [], // Root elemet | |
curr = root, // Current array element being appended to | |
m_stack = [root,], // Stack of "m" elements | |
m_count = [parts.length,]; // Number of elements to put under each level | |
parts.forEach(function(el) { | |
var kind = el.substr(1, 1), | |
value = el.substr(2); | |
// Decrement all the m_counts | |
for (var i = 0; i < m_count.length; i++) { | |
m_count[i]--; | |
} | |
if (kind === 'm') { // Add a new array to capture coming values | |
var new_arr = []; | |
m_count.push(value); | |
curr.push(new_arr); | |
m_stack.push(new_arr); | |
curr = new_arr; | |
} | |
else { | |
if (kind == 'b') { // Assuming these are boolean | |
curr.push(value == '1'); | |
} | |
else if (kind == 'd' || kind == 'f') { // Float or double | |
curr.push(parseFloat(value)); | |
} | |
else if (kind == 'i' || kind == 'u' || kind == 'e') { // Integer, unsigned or enum as int | |
curr.push(parseInt(value)); | |
} | |
else { // Store anything else as a string | |
curr.push(value); | |
} | |
} | |
// Pop off all the arrays that have their values already | |
while (m_count[m_count.length - 1] === 0) { | |
m_stack.pop(); | |
m_count.pop(); | |
curr = m_stack[m_stack.length - 1]; | |
} | |
}); | |
console.log(util.inspect(root, { depth: null })); |
Do you have encode function for this?
Do you have encode function for this?
if you don't mind the PHP language, check this: https://github.com/richardDobron/google-maps-data-parameter-parser
Do you have encode function for this?
if you don't mind the PHP language, check this: https://github.com/richardDobron/google-maps-data-parameter-parser
Thank you, i was tried your script, it's works, however, after decode, then encode, the encoded output is not the same with first string, is it normal?
Try to create an issue, I have been using this script for a long time on different addresses, and it works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice script, thanks for this!
It doesn't seem to work with this Google Maps URL which has a
data
value of:The result is:
Which doesn't look like it parsed correctly, does it?
I didn't manipulate the URL in any way, it's generated simply by:
hotels near washington dc
Hilton
,Hyatt
,IHG
,Marriott
and click Done.