Skip to content

Instantly share code, notes, and snippets.

@carpiediem
Last active September 29, 2020 08:40
Show Gist options
  • Save carpiediem/78e8bae8b09a707e670e4ed4d3f94fca to your computer and use it in GitHub Desktop.
Save carpiediem/78e8bae8b09a707e670e4ed4d3f94fca to your computer and use it in GitHub Desktop.
Solving the Travelling Salesman Problem with Google My Maps Markers and Optimap
$(function() {
$('form#stepone').submit(function() {
console.log('submitted');
var url = $('#inputUrl').val();
var midSearch = /mid=([^&]+)/.exec(url);
if (midSearch.length!==2) {
$('#warning').show();
return false;
}
window.location = 'https://www.google.com/maps/d/u/0/kml?forcekml=true&mid='+midSearch[1];
$('form#steptwo').show();
$('form#stepone').hide();
return false;
});
$('input#inputFile').change(function() {
if (!window.File || !window.FileReader || !window.FileList || !window.Blob) {
alert('The File APIs are not fully supported in this browser.');
return;
}
input = document.getElementById('inputFile');
if (!input) {
alert("Um, couldn't find the fileinput element.");
}
else if (!input.files) {
alert("This browser doesn't seem to support the `files` property of file inputs.");
}
else if (!input.files[0]) {
alert("Please select a file before clicking 'Load'");
}
else {
file = $(this)[0].files[0];
fr = new FileReader();
fr.onload = function () {
var base64 = fr.result.replace(/data:[^;]+;base64,/, '');
var base64_decode = atob(base64);
var regex = /\<coordinates\>\s*(\d+\.\d+,\d+\.\d+),\d+\.?\d*\s*\<\/coordinates\>/g;
var list = '';
while ((m = regex.exec(base64_decode)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
var lnglat = m[1].split(',');
list += lnglat[1] + ', ' + lnglat[0] + '\r\n';
}
$('#list').text(list);
$('#stepthree').show();
$('form#steptwo').hide();
};
fr.readAsDataURL(file);
}
});
});
<html>
<head>
<script src=""></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="converter.js"></script>
<title>Solving the Travelling Salesman Problem with Google My Maps Markers and Optimap</title>
</head>
<body>
<div class="container-fluid">
<div class="row mt-2">
<div class="col-sm-4"></div>
<div class="col-sm-4">
<form id="stepone">
<div class="form-group">
<label for="inputEmail4">URL of Google My Map</label>
<input type="url" class="form-control" id="inputUrl" placeholder="Map URL">
</div>
<button type="submit" class="btn btn-primary btn-lg btn-block">Download KML</button>
</form>
<form id="steptwo" style="display: none;">
<p>
If everything is working, a KML file was just downloaded. Either drag the file into the field below or click the <i>Choose File</i> button to find it on your computer.
</p>
<div class="form-group">
<label for="inputEmail4">KML file</label>
<input type="file" class="form-control" id="inputFile">
</div>
</form>
<div id="stepthree" style="display: none;">
<p>
Now, copy the list below, then open <a href="http://www.gebweb.net/optimap/" target="_blank">OptiMap</a> and select <i>Bulk Add Addresses</i> from the page header.
</p>
<textarea id="list"></textarea>
</div>
</div>
</div>
</div>
</body>

Copyright (c) 2018 Ryan Carpenter

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment