Code Monkey home page Code Monkey logo

leaflet.zoomfs's Introduction

For a plugin that handles fullscreen exclusively see Leaflet.fullscreen.

Leaflet.Control.ZoomFS

This is a simple extension of Leaflet.Control.Zoom that adds a fullscreen button above the zoom in and zoom out controls.

You can make a Leaflet map fullscreen programatically by changing the CSS of the map container. This extension simply integrates that functionality into the Leaflet interface.

Usage

Be sure to include the leaflet.zoomfs.js script somewhere after Leaflet is loaded.

<script src="http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.js"></script>
<script src="leaflet.zoomfs.js"></script>

Do all your normal Leaflet initialization stuff, except make sure that you initialize the map without the default zoom controls:

<!-- map container -->
<div id="map"></div>

// init map
var map = new L.Map('map', { zoomControl:false });

Then, instantiate the ZoomFS control and add it to the map:

var zoomFS = new L.Control.ZoomFS(); 
map.addControl(zoomFS);

Events

There are 2 events you can bind to: enterFullscreen and exitFullscreen. Note that these events are triggered on the Map object; not ZoomFS.

map.on('enterFullscreen', function(){
  if(window.console) window.console.log('enterFullscreen');
});

map.on('exitFullscreen', function(){
  if(window.console) window.console.log('exitFullscreen');
});

Style

This extension applies the following inline styles to make the map fullscreen. We also apply a class of leaflet-fullscreen to the map container.

<div id="map" class="leaflet-container leaflet-fullscreen" style="position: fixed; left: 0px; top: 0px; width: 100%; height: 100%;">

No style is applied to the fullscreen button itself. Basic style is handled internally by Leaflet.Control.Zoom, but you'll need to add your own button style:

.leaflet-control-fullscreen {
  background-image: url(your/amazing/icon.png);
}

Caveat

All inline style on your map container will be removed when exitFullscreen is triggered. position:relative is re-applied automatically if you do not have a position declared in your base styles (as this is required by Leaflet). A height is required by Leaflet core, and it should be declared in your base CSS (not inline) like so:

#map {
  position: relative;
  height: 400px;
}

Notes

  • This extension was built and tested for Leaflet version 0.5.1. If you need this to work with Leaflet 0.4.4 or Leaflet 0.3.1 there are old versions in tags
  • If you want the fullscreen button below the zoom in and zoom out buttons simply switch the order in which the controls are added in leaflet.zoomfs.js.

leaflet.zoomfs's People

Contributors

elidupuis avatar tmcw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

leaflet.zoomfs's Issues

Doesn't work anymore with leaflet 0.5

Crashes on function below with error: Microsoft JScript runtime error: Object doesn't support property or method 'appendChild'

Location in leaflet.js: return o.className=i,n&&n.appendChild(o),o

onAdd: function (map)
{

    this._map = map;
    this._isFullscreen = false;
    this._container = L.DomUtil.create('div', 'leaflet-control-zoom');

    this._zoomInButton = this._createButton('Zoom in', 'leaflet-control-zoom-in', this._container, this._map.zoomIn, this._map);
    this._zoomOutButton = this._createButton('Zoom out', 'leaflet-control-zoom-out', this._container, this._map.zoomOut, this._map);

    // ideally the fullscreen function should reside in the Map class, but this will do for now...
    this._fullScreenButton = this._createButton('Full Screen', 'leaflet-control-fullscreen', this._container, this.fullscreen, this);

    // reorder this as you like (ie: place fullscreen button below zoom buttons)
    this._container.appendChild(this._fullScreenButton);
    this._container.appendChild(this._zoomInButton);
    this._container.appendChild(this._zoomOutButton);

    return this._container;

},

Any idea?

Thanks

Ingmar

should only add inline style if required

...check to see if the user has a default position in their CSS and if so, do not apply position:relative on exitFullscreen. this will allow for easier custom positioning such as absolute or fixed in a unique layout.

no need to store css styles

All styles applied via JS are inline, so storing the base style is unnecessary. Just remove the styles you've applied via JS and the user's base CSS styles will take effect again...

If they have inline styles that get overridden—it's there own fault :)

Broken on Leaflet 0.4.4

Doesn't seem to work with Leaflet 0.4.4. Throws an error: " n.appendChild is not a function".

Any idea how to fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.