Skip to content

Instantly share code, notes, and snippets.

@wallneradam
Forked from jeteon/google-maps-data-parser.js
Last active May 10, 2017 14:29
Show Gist options
  • Save wallneradam/157fbf086bbc45536f113ea250e88df9 to your computer and use it in GitHub Desktop.
Save wallneradam/157fbf086bbc45536f113ea250e88df9 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
*/
// Data string to be parsed
var str = '!1m5!1m4!1i16!2i36231!3i22928!4i256!2m3!1e0!2sm!3i379068808!3m14!2shu-HU!3sUS!5e18!12m1!1e68!12m3!1e37!2m1!1ssmartmaps!12m4!1e26!2m2!1sstyles!2zcy5lOmd8cC5jOiNmZmViZTNjZCxzLmU6bC50LmZ8cC5jOiNmZjUyMzczNSxzLmU6bC50LnN8cC5jOiNmZmY1ZjFlNixzLnQ6MXxzLmU6Zy5zfHAuYzojZmZjOWIyYTYscy50OjIxfHMuZTpnLnN8cC5jOiNmZmRjZDJiZSxzLnQ6MjF8cy5lOmwudC5mfHAuYzojZmZhZTllOTAscy50OjgxfHAudjpvZmYscy50OjgyfHMuZTpnfHAuYzojZmZkZmQyYWUscy50OjJ8cy5lOmd8cC5jOiNmZmRmZDJhZSxzLnQ6MnxzLmU6bC50LmZ8cC5jOiNmZjkzODE3YyxzLnQ6MzN8cC52Om9mZixzLnQ6NDB8cy5lOmcuZnxwLmM6I2ZmYTViMDc2LHMudDo0MHxzLmU6bC50fHAudjpvZmYscy50OjQwfHMuZTpsLnQuZnxwLmM6I2ZmNDQ3NTMwLHMudDozfHMuZTpnfHAuYzojZmZmNWYxZTYscy50OjUwfHMuZTpnfHAuYzojZmZmZGZjZjgscy50OjQ5fHMuZTpnfHAuYzojZmZmOGM5Njcscy50OjQ5fHMuZTpnLnN8cC5jOiNmZmU5YmM2MixzLnQ6Nzg1fHMuZTpnfHAuYzojZmZlOThkNTgscy50Ojc4NXxzLmU6Zy5zfHAuYzojZmZkYjg1NTUscy50OjUxfHMuZTpnLmZ8cC5zOjIwLHMudDo1MXxzLmU6bC50LmZ8cC5jOiNmZjgwNmI2MyxzLnQ6NjV8cy5lOmd8cC5jOiNmZmQwYTA4MixzLnQ6NjV8cy5lOmwudC5mfHAuYzojZmY4ZjdkNzcscy50OjY1fHMuZTpsLnQuc3xwLmM6I2ZmZWJlM2NkLHMudDo2NnxzLmU6Z3xwLmM6I2ZmZGZkMmFlLHMudDo2fHMuZTpnLmZ8cC5jOiNmZmI5ZDNjMixzLnQ6NnxzLmU6bC50LmZ8cC5jOiNmZjkyOTk4ZA!4e0';
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 m = /^(\d+)(\w)(.*)$/.exec(el),
idx = m[1],
kind = m[2],
value = m[3];
// 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(JSON.stringify(root, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment