Skip to content

Instantly share code, notes, and snippets.

@jeteon
Last active February 7, 2024 14:28
Show Gist options
  • Save jeteon/e71fa21c1feb48fe4b5eeec045229a0c to your computer and use it in GitHub Desktop.
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.
'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 }));
@zack-the-worker
Copy link

@zack-the-worker

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?

@richardDobron
Copy link

richardDobron commented Dec 29, 2023

@zack-the-worker

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