Sync / Minimap
HTML
<script src="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.6.4/leaflet.js"></script>
<div id="map1">
</div>
<div id="map2">
</div>
<script>
L.Control.MiniMap = L.Control.extend({
options: {
position: 'bottomright',
toggleDisplay: false,
zoomLevelOffset: -5,
zoomLevelFixed: false,
zoomAnimation: false,
autoToggleDisplay: false,
width: 275,
height: 150
},
hideText: 'Hide MiniMap',
showText: 'Show MiniMap',
//layer is the map layer to be shown in the minimap
initialize: function (layer, options) {
L.Util.setOptions(this, options);
this._layer = layer;
},
onAdd: function (map) {
this._mainMap = map;
//Creating the container and stopping events from spilling through to the main map.
this._container = L.DomUtil.create('div', 'leaflet-control-minimap');
this._container.style.width = this.options.width + 'px';
this._container.style.height = this.options.height + 'px';
L.DomEvent.disableClickPropagation(this._container);
L.DomEvent.on(this._container, 'mousewheel', L.DomEvent.stopPropagation);
this._miniMap = new L.Map(this._container,
{
attributionControl: false,
zoomControl: false,
zoomAnimation: this.options.zoomAnimation,
autoToggleDisplay: this.options.autoToggleDisplay,
touchZoom: !this.options.zoomLevelFixed,
scrollWheelZoom: !this.options.zoomLevelFixed,
doubleClickZoom: !this.options.zoomLevelFixed,
boxZoom: !this.options.zoomLevelFixed,
crs: map.options.crs
});
this._miniMap.addLayer(this._layer);
//These bools are used to prevent infinite loops of the two maps notifying each other that they've moved.
this._mainMapMoving = false;
this._miniMapMoving = false;
//Keep a record of this to prevent auto toggling when the user explicitly doesn't want it.
this._userToggledDisplay = false;
this._minimized = false;
if (this.options.toggleDisplay) {
this._addToggleButton();
}
this._miniMap.whenReady(L.Util.bind(function () {
this._aimingRect =...
CSS
#map1 {
width:50%;
height:500px;
float:left;
background:#ccc;
}
#map2 {
width:50%;
height:500px;
float:left;
background:#888;
}
.leaflet-control-minimap {
border:solid rgba(255, 255, 255, 1.0) 3px;
box-shadow: 0 1px 7px #999;
background: #f8f8f9;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
transition: all .2s;
}
.leaflet-control-minimap a {
background-color: rgba(255, 255, 255, 1.0);
background-position: 1px 2px;
background-repeat: no-repeat;
display: block;
outline: none;
z-index: 99999;
transition: all .2s;
}
.leaflet-control-minimap a.minimized {
-webkit-transform: rotate(180deg);
transform: rotate(180deg);
}
.leaflet-control-minimap-toggle-display {
background-image: url("images/toggle.png");
border-radius: 2px 0px 2px 0px;
height: 19px;
width: 19px;
position: absolute;
bottom: 0;
right: 0;
}
/* required styles */
.leaflet-map-pane,
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow,
.leaflet-tile-pane,
.leaflet-tile-container,
.leaflet-overlay-pane,
.leaflet-shadow-pane,
.leaflet-marker-pane,
.leaflet-popup-pane,
.leaflet-overlay-pane svg,
.leaflet-zoom-box,
.leaflet-image-layer,
.leaflet-layer {
position: absolute;
left: 0;
top: 0;
}
.leaflet-container {
overflow: hidden;
-ms-touch-action: none;
}
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow {
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
-webkit-user-drag: none;
}
.leaflet-marker-icon,
.leaflet-marker-shadow {
display: block;
}
/* map is broken in FF if you have max-width: 100% on tiles */
.leaflet-container img {
max-width: none !important;
}
/* stupid Android 2 doesn't understand "max-width: none" properly */
.leaflet-container img.leaflet-image-layer {
max-width: 15000px !important;
}
.leaflet-tile {
filter: inherit;
visibility: hidden;
}
.leaflet-tile-loaded...
JavaScript
var map1 = L.map('map1').setView([51.505, -0.09], 16);
var map2 = L.map('map2').setView([51.505, -0.09], 16);
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map1);
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map2);
//sync
map1.sync(map2);
map2.sync(map1);
//minimap
new L.Control.MiniMap(L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png')).addTo(map2);