Skip to content

Instantly share code, notes, and snippets.

@xantiagoma
Created July 29, 2020 04:20
Show Gist options
  • Save xantiagoma/5d1b4dfdffb5de8e55e78500d3fd71cb to your computer and use it in GitHub Desktop.
Save xantiagoma/5d1b4dfdffb5de8e55e78500d3fd71cb to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang="en">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-64554054-15"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-64554054-15');
</script>
<meta charset="utf-8">
<title>Decode & visualize encoded polylines on OpenStreetMaps</title>
<meta name="description" content="Online tool to visualize, decode and edit polylines using Leaflet and OpenStreetMap">
<meta name="author" content="60devs.com">
<meta name="keywords" content="polyline editor decode encode">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
<![endif]-->
<meta property="og:url" content="https://open-polyline-decoder.60devs.com" />
<meta property="og:title" content="Decode & visualize encoded polylines on OpenStreetMaps" />
<meta property="og:description" content="Online tool to visualize, decode and edit polylines using Leaflet and OpenStreetMap" />
<meta property="og:image" content="https://open-polyline-decoder.60devs.com/preview.png" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@60devs" />
<meta name="twitter:title" content="Polyline Decoder" />
<meta name="twitter:description" content="Decode & visualize encoded polylines on OpenStreetMaps" />
<meta name="twitter:image" content="https://open-polyline-decoder.60devs.com/preview.png" />
<meta name="twitter:image:alt" content="Decode & visualize encoded polylines on OpenStreetMaps" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css" integrity="sha256-HxaKz5E/eBbvhGMNwhWRPrAR9i/lG1JeT4mD6hCQ7s4=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css"
integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ=="
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"
integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log=="
crossorigin=""></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet.pm@latest/dist/leaflet.pm.css" />
<script src="https://unpkg.com/leaflet.pm@latest/dist/leaflet.pm.min.js"></script>
<script src="https://unpkg.com/@mapbox/polyline@0.2.0/src/polyline.js"></script>
<style type="text/css" media="screen">
body {
height: 100vh;
}
main {
height: 100vh;
display: flex;
flex-direction: row;
}
header {
height: 7rem;
}
#map {
width: 67%;
height: 100%;
}
#control {
width: 33%;
}
.tabs {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-evenly;
align-content: center;
margin: 10px;
}
.tabs a {
flex: 0 1 33%;
max-width: 100%;
text-align: center;
display: block;
padding: 10px 30px;
align-content: center;
text-decoration: none;
cursor: pointer;
color: white;
font-weight: bold;
box-sizing: border-box;
}
.tabs a.active {
border-bottom: 5px solid #006BA6;
}
.tabs a:first-child {
background-color: #0496FF;
}
.tabs a:nth-child(2) {
background-color: #FFBC42;
color: #333;
}
.tabs a:nth-child(3) {
background-color: #D81159;
}
#io {
box-sizing: border-box;
width: calc(100% - 20px);
height: 400px;
margin: 10px;
overflow-x: hidden;
overflow-y: auto;
}
#error {
margin: 10px;
color: red;
}
.examples {
margin: 10px;
}
</style>
</head>
<body>
<main>
<div id="map">
</div>
<div id="control">
<div class="tabs">
<a class="tab active" href="#" onclick="return chaneCurInputMode(0);">Encoded Polyline</a>
<a class="tab" href="#" onclick="return chaneCurInputMode(1);">LatLng Array</a>
<a class="tab" href="#" onclick="return chaneCurInputMode(2);">LngLat Array</a>
</div>
<textarea autofocus id="io" placeholder="paste an encoded polyline here"></textarea>
<p id="error"></p>
<p class="examples">
Examples: <a href="#" onclick="return setCurPolyline('_}l_IuvypAuNiKwC}BBW?_@E[QYKCYDMLIREV?VBVFRJNgPr}@');">trip polyline</a>
</p>
<p class="examples">
Built by <a href="https://twitter.com/orKoN">Alex Rudenko</a> in 2017. Relies on <a href="https://github.com/codeofsumit/leaflet.pm">leaflet.pm</a>, <a href="http://leafletjs.com/">leaflet</a> and <a href="https://github.com/mapbox/polyline">polyline</a>.
</p>
</div>
<script type="text/javascript">
function setupMap() {
var map = L
.map('map')
.setView([0, 90], 2);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1Ijoib3Jrb24iLCJhIjoiY2l5dmUyYndvMDAycjMycXc3NGdseDVrdyJ9.7R__L5LZAoWSeUi_YM021A', {
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: 'your.mapbox.access.token'
}).addTo(map);
map.pm.addControls({
position: 'topleft',
drawMarker: false,
drawPolyline: true,
drawRectangle: false,
drawPolygon: false,
drawCircle: false,
cutPolygon: false,
editMode: true,
removalMode: true,
});
map.pm.setPathOptions({
color: 'blue',
width: 5,
});
return map;
}
var io = document.getElementById('io');
var errOut = document.getElementById('error');
var map = setupMap();
var curPolyline = [];
var curMapboxPolyline = null;
var curInputMode = 0;
function disableDrawLine() {
map.pm.addControls({
drawPolyline: false,
});
}
function enableDrawLine() {
map.pm.addControls({
drawPolyline: true,
});
}
function syncPolyline() {
if (curMapboxPolyline) {
curPolyline = curMapboxPolyline.getLatLngs().map(function(item) {
return [item.lat, item.lng];
});
} else {
curPolyline = [];
}
}
function onPmEdit() {
syncPolyline();
render();
}
function onPmCreate(e) {
curMapboxPolyline = e.layer;
curMapboxPolyline.on('pm:edit', onPmEdit);
disableDrawLine();
syncPolyline();
render();
}
function onPmRemove(e) {
curMapboxPolyline.off('pm:edit', onPmEdit);
curMapboxPolyline = null;
enableDrawLine();
syncPolyline();
render();
}
function onInput(e) {
const val = e.target.value.trim();
if (val === '') {
if (curMapboxPolyline) {
map.removeLayer(curMapboxPolyline);
}
enableDrawLine();
} else {
try {
switch (curInputMode) {
case 0:
curPolyline = polyline.decode(val);
break;
case 1:
curPolyline = JSON.parse(val);
break;
case 2:
curPolyline = JSON.parse(val).map(function(item) {
return [item[1], item[0]];
});
break;
}
if (!curMapboxPolyline) {
curMapboxPolyline = L.polyline(curPolyline, {
color: 'blue',
weight: 5,
}).addTo(map);
curMapboxPolyline.on('pm:edit', onPmEdit);
map.fitBounds(curMapboxPolyline.getBounds());
disableDrawLine();
} else {
curMapboxPolyline.setLatLngs(curPolyline.map(item => {
return new L.LatLng(item[0], item[1]);
}));
map.fitBounds(curMapboxPolyline.getBounds());
}
errOut.innerHTML = '';
} catch (err) {
errOut.innerHTML = err.message;
}
}
}
map.on('pm:create', onPmCreate);
map.on('pm:remove', onPmRemove);
io.oninput = onInput;
function setCurPolyline(encp) {
curInputMode = 0;
onInput({
target: {
value: encp,
},
});
render();
return false;
}
function render() {
let val = '';
switch (curInputMode) {
case 0:
val = polyline.encode(curPolyline);
break;
case 1:
val = JSON.stringify(curPolyline, null, 2);
break;
case 2:
val = JSON.stringify(curPolyline.map(function(item) {
return [item[1], item[0]];
}), null, 2);
break;
}
if (io.value !== val) {
io.value = val;
}
const tags = document.getElementsByClassName('tab');
for (var i = 0; i<tags.length; i++) {
if (i === curInputMode) {
tags[i].classList.add('active');
} else {
tags[i].classList.remove('active');
}
}
return false;
}
function setDisplay(str) {
const selectionStart = io.selectionStart;
const selectionEnd = io.selectionEnd;
console.log(selectionStart, selectionEnd)
io.value = str;
io.setSelectionRange(selectionStart, selectionEnd);
}
function chaneCurInputMode(displayType) {
curInputMode = displayType;
render();
return false;
}
</script>
</main>
<!-- MIT License
Copyright (c) 2017 Oleksii Rudenko
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. -->
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment