Skip to content

Instantly share code, notes, and snippets.

@benheb
Last active October 2, 2017 16:55
Show Gist options
  • Save benheb/0359091f537b559c12a908a63af77cbf to your computer and use it in GitHub Desktop.
Save benheb/0359091f537b559c12a908a63af77cbf to your computer and use it in GitHub Desktop.
Mapbox Map Pan Issue When Moving to Child Window
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.1/mapbox-gl.css' rel='stylesheet' />
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; transition: all 0.3s; }
.button {
color: #444;
background-color: #fff;
padding: 0.3rem;
margin: 1em;
position: absolute;
right: 1em;
top: 1em;
border-radius: 0.5em;
cursor: pointer;
z-index: 100;
}
</style>
</head>
<body>
<div class="map-wrapper">
<div id='map'>
<div class="button" id="popout">Popout Map</div>
</div>
</div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4M29iazA2Z2gycXA4N2pmbDZmangifQ.-g_vE53SD2WrJ6tFX7QHmA';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v9', //stylesheet location
center: [-74.50, 40], // starting position
zoom: 9 // starting zoom
});
$('#popout').on('click', function(e) {
console.log('click me');
var orphanMap = $('#map').parent();
var CHILD_WINDOW =window.open('map-popout', '#map', 'status=0,toolbar=0,location=no,width=500,height=300');
var doc = CHILD_WINDOW.document;
doc.open();
doc.write($('head')[0].innerHTML);
doc.close();
//slight timeout to make sure child window has rendered successfully
//TODO remove need for this
setTimeout(() => {
$(doc.body).append(orphanMap);
map.resize();
},300);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment