2 Copyright (c) 2016 Dominik Moritz
 
   4 This file is part of the leaflet locate control. It is licensed under the MIT license.
 
   5 You can find the project at: https://github.com/domoritz/leaflet-locatecontrol
 
   7 (function (factory, window) {
 
   8      // see https://github.com/Leaflet/Leaflet/blob/master/PLUGIN-GUIDE.md#module-loaders
 
   9      // for details on how to structure a leaflet plugin.
 
  11     // define an AMD module that relies on 'leaflet'
 
  12     if (typeof define === 'function' && define.amd) {
 
  13         define(['leaflet'], factory);
 
  15     // define a Common JS module that relies on 'leaflet'
 
  16     } else if (typeof exports === 'object') {
 
  17         if (typeof window !== 'undefined' && window.L) {
 
  18             module.exports = factory(L);
 
  20             module.exports = factory(require('leaflet'));
 
  24     // attach your plugin to the global 'L' variable
 
  25     if(typeof window !== 'undefined' && window.L){
 
  26         window.L.Control.Locate = factory(L);
 
  29     var LocateControl = L.Control.extend({
 
  31             /** Position of the control */
 
  33             /** The layer that the user's location should be drawn on. By default creates a new layer. */
 
  36              * Automatically sets the map view (zoom and pan) to the user's location as it updates.
 
  37              * While the map is following the user's location, the control is in the `following` state,
 
  38              * which changes the style of the control and the circle marker.
 
  41              *  - false: never updates the map view when location changes.
 
  42              *  - 'once': set the view when the location is first determined
 
  43              *  - 'always': always updates the map view when location changes.
 
  44              *              The map view follows the users location.
 
  45              *  - 'untilPan': (default) like 'always', except stops updating the
 
  46              *                view if the user has manually panned the map.
 
  47              *                The map view follows the users location until she pans.
 
  50             /** Keep the current map zoom level when setting the view and only pan. */
 
  51             keepCurrentZoomLevel: false,
 
  52             /** Smooth pan and zoom to the location of the marker. Only works in Leaflet 1.0+. */
 
  55              * The user location can be inside and outside the current view when the user clicks on the
 
  56              * control that is already active. Both cases can be configures separately.
 
  57              * Possible values are:
 
  58              *  - 'setView': zoom and pan to the current location
 
  59              *  - 'stop': stop locating and remove the location marker
 
  62                 /** What should happen if the user clicks on the control while the location is within the current view. */
 
  64                 /** What should happen if the user clicks on the control while the location is outside the current view. */
 
  68              * If set, save the map bounds just before centering to the user's
 
  69              * location. When control is disabled, set the view back to the
 
  70              * bounds that were saved.
 
  72             returnToPrevBounds: false,
 
  73             /** If set, a circle that shows the location accuracy is drawn. */
 
  75             /** If set, the marker at the users' location is drawn. */
 
  77             /** The class to be used to create the marker. For example L.CircleMarker or L.Marker */
 
  78             markerClass: L.CircleMarker,
 
  79             /** Accuracy circle style properties. */
 
  87             /** Inner marker style properties. */
 
  97              * Changes to accuracy circle and inner marker while following.
 
  98              * It is only necessary to provide the properties that should change.
 
 100             followCircleStyle: {},
 
 103                 // fillColor: '#FFB000'
 
 105             /** The CSS class for the icon. For example fa-location-arrow or fa-map-marker */
 
 106             icon: 'fa fa-map-marker',
 
 107             iconLoading: 'fa fa-spinner fa-spin',
 
 108             /** The element to be created for icons. For example span or i */
 
 109             iconElementTag: 'span',
 
 110             /** Padding around the accuracy circle. */
 
 111             circlePadding: [0, 0],
 
 112             /** Use metric units. */
 
 114             /** This event is called in case of any location error that is not a time out error. */
 
 115             onLocationError: function(err, control) {
 
 119              * This even is called when the user's location is outside the bounds set on the map.
 
 120              * The event is called repeatedly when the location changes.
 
 122             onLocationOutsideMapBounds: function(control) {
 
 124                 alert(control.options.strings.outsideMapBoundsMsg);
 
 126             /** Display a pop-up when the user click on the inner marker. */
 
 129                 title: "Show me where I am",
 
 130                 metersUnit: "meters",
 
 132                 popup: "You are within {distance} {unit} from this point",
 
 133                 outsideMapBoundsMsg: "You seem located outside the boundaries of the map"
 
 135             /** The default options passed to leaflets locate method. */
 
 138                 watch: true,  // if you overwrite this, visualization cannot be updated
 
 139                 setView: false // have to set this to false because we have to
 
 140                                // do setView manually
 
 144         initialize: function (options) {
 
 145             // set default options if nothing is set (merge one step deep)
 
 146             for (var i in options) {
 
 147                 if (typeof this.options[i] === 'object') {
 
 148                     L.extend(this.options[i], options[i]);
 
 150                     this.options[i] = options[i];
 
 154             // extend the follow marker style and circle from the normal style
 
 155             this.options.followMarkerStyle = L.extend({}, this.options.markerStyle, this.options.followMarkerStyle);
 
 156             this.options.followCircleStyle = L.extend({}, this.options.circleStyle, this.options.followCircleStyle);
 
 160          * Add control to map. Returns the container for the control.
 
 162         onAdd: function (map) {
 
 163             var container = L.DomUtil.create('div',
 
 164                 'leaflet-control-locate leaflet-bar leaflet-control');
 
 166             this._layer = this.options.layer || new L.LayerGroup();
 
 167             this._layer.addTo(map);
 
 168             this._event = undefined;
 
 170             this._link = L.DomUtil.create('a', 'leaflet-bar-part leaflet-bar-part-single', container);
 
 171             this._link.href = '#';
 
 172             this._link.title = this.options.strings.title;
 
 173             this._icon = L.DomUtil.create(this.options.iconElementTag, this.options.icon, this._link);
 
 176                 .on(this._link, 'click', L.DomEvent.stopPropagation)
 
 177                 .on(this._link, 'click', L.DomEvent.preventDefault)
 
 178                 .on(this._link, 'click', this._onClick, this)
 
 179                 .on(this._link, 'dblclick', L.DomEvent.stopPropagation);
 
 181             this._resetVariables();
 
 183             this._map.on('unload', this._unload, this);
 
 189          * This method is called when the user clicks on the control.
 
 191         _onClick: function() {
 
 192             this._justClicked = true;
 
 193             this._userPanned = false;
 
 194             this._prevBounds = null;
 
 196             if (this._active && !this._event) {
 
 197                 // click while requesting
 
 199             } else if (this._active && this._event !== undefined) {
 
 200                 var behavior = this._map.getBounds().contains(this._event.latlng) ?
 
 201                     this.options.clickBehavior.inView : this.options.clickBehavior.outOfView;
 
 208                         if (this.options.returnToPrevBounds) {
 
 209                             var f = this.options.flyTo ? this._map.flyToBounds : this._map.fitBounds;
 
 210                             f.bind(this._map)(this._prevBounds);
 
 215                 if (this.options.returnToPrevBounds) {
 
 216                   this._prevBounds = this._map.getBounds();
 
 221             this._updateContainerStyle();
 
 226          * - activates the engine
 
 227          * - draws the marker (if coordinates available)
 
 233                 this._drawMarker(this._map);
 
 235                 // if we already have a location but the user clicked on the control
 
 236                 if (this.options.setView) {
 
 240             this._updateContainerStyle();
 
 245          * - deactivates the engine
 
 246          * - reinitializes the button
 
 247          * - removes the marker
 
 252             this._cleanClasses();
 
 253             this._resetVariables();
 
 255             this._removeMarker();
 
 259          * This method launches the location engine.
 
 260          * It is called before the marker is updated,
 
 261          * event if it does not mean that the event will be ready.
 
 263          * Override it if you want to add more functionalities.
 
 264          * It should set the this._active to true and do nothing if
 
 265          * this._active is true.
 
 267         _activate: function() {
 
 269                 this._map.locate(this.options.locateOptions);
 
 272                 // bind event listeners
 
 273                 this._map.on('locationfound', this._onLocationFound, this);
 
 274                 this._map.on('locationerror', this._onLocationError, this);
 
 275                 this._map.on('dragstart', this._onDrag, this);
 
 280          * Called to stop the location engine.
 
 282          * Override it to shutdown any functionalities you added on start.
 
 284         _deactivate: function() {
 
 285             this._map.stopLocate();
 
 286             this._active = false;
 
 288             // unbind event listeners
 
 289             this._map.off('locationfound', this._onLocationFound, this);
 
 290             this._map.off('locationerror', this._onLocationError, this);
 
 291             this._map.off('dragstart', this._onDrag, this);
 
 295          * Zoom (unless we should keep the zoom level) and an to the current view.
 
 297         setView: function() {
 
 299             if (this._isOutsideMapBounds()) {
 
 300                 this.options.onLocationOutsideMapBounds(this);
 
 302                 if (this.options.keepCurrentZoomLevel) {
 
 303                     var f = this.options.flyTo ? this._map.flyTo : this._map.panTo;
 
 304                     f.bind(this._map)([this._event.latitude, this._event.longitude]);
 
 306                     var f = this.options.flyTo ? this._map.flyToBounds : this._map.fitBounds;
 
 307                     f.bind(this._map)(this._event.bounds, {
 
 308                         padding: this.options.circlePadding,
 
 309                         maxZoom: this.options.locateOptions.maxZoom
 
 316          * Draw the marker and accuracy circle on the map.
 
 318          * Uses the event retrieved from onLocationFound from the map.
 
 320         _drawMarker: function() {
 
 321             if (this._event.accuracy === undefined) {
 
 322                 this._event.accuracy = 0;
 
 325             var radius = this._event.accuracy;
 
 326             var latlng = this._event.latlng;
 
 328             // circle with the radius of the location's accuracy
 
 329             if (this.options.drawCircle) {
 
 330                 var style = this._isFollowing() ? this.options.followCircleStyle : this.options.circleStyle;
 
 333                     this._circle = L.circle(latlng, radius, style).addTo(this._layer);
 
 335                     this._circle.setLatLng(latlng).setRadius(radius).setStyle(style);
 
 340             if (this.options.metric) {
 
 341                 distance = radius.toFixed(0);
 
 342                 unit =  this.options.strings.metersUnit;
 
 344                 distance = (radius * 3.2808399).toFixed(0);
 
 345                 unit = this.options.strings.feetUnit;
 
 348             // small inner marker
 
 349             if (this.options.drawMarker) {
 
 350                 var mStyle = this._isFollowing() ? this.options.followMarkerStyle : this.options.markerStyle;
 
 353                     this._marker = new this.options.markerClass(latlng, mStyle).addTo(this._layer);
 
 355                     this._marker.setLatLng(latlng).setStyle(mStyle);
 
 359             var t = this.options.strings.popup;
 
 360             if (this.options.showPopup && t && this._marker) {
 
 362                     .bindPopup(L.Util.template(t, {distance: distance, unit: unit}))
 
 363                     ._popup.setLatLng(latlng);
 
 368          * Remove the marker from map.
 
 370         _removeMarker: function() {
 
 371             this._layer.clearLayers();
 
 372             this._marker = undefined;
 
 373             this._circle = undefined;
 
 377          * Unload the plugin and all event listeners.
 
 378          * Kind of the opposite of onAdd.
 
 380         _unload: function() {
 
 382             this._map.off('unload', this._unload, this);
 
 386          * Calls deactivate and dispatches an error.
 
 388         _onLocationError: function(err) {
 
 389             // ignore time out error if the location is watched
 
 390             if (err.code == 3 && this.options.locateOptions.watch) {
 
 395             this.options.onLocationError(err, this);
 
 399          * Stores the received event and updates the marker.
 
 401         _onLocationFound: function(e) {
 
 402             // no need to do anything if the location has not changed
 
 404                 (this._event.latlng.lat === e.latlng.lat &&
 
 405                  this._event.latlng.lng === e.latlng.lng &&
 
 406                      this._event.accuracy === e.accuracy)) {
 
 411                 // we may have a stray event
 
 418             this._updateContainerStyle();
 
 420             switch (this.options.setView) {
 
 422                     if (this._justClicked) {
 
 427                     if (!this._userPanned) {
 
 435                     // don't set the view
 
 439             this._justClicked = false;
 
 443          * When the user drags. Need a separate even so we can bind and unbind even listeners.
 
 445         _onDrag: function() {
 
 446             // only react to drags once we have a location
 
 448                 this._userPanned = true;
 
 449                 this._updateContainerStyle();
 
 455          * Compute whether the map is following the user location with pan and zoom.
 
 457         _isFollowing: function() {
 
 462             if (this.options.setView === 'always') {
 
 464             } else if (this.options.setView === 'untilPan') {
 
 465                 return !this._userPanned;
 
 470          * Check if location is in map bounds
 
 472         _isOutsideMapBounds: function() {
 
 473             if (this._event === undefined) {
 
 476             return this._map.options.maxBounds &&
 
 477                 !this._map.options.maxBounds.contains(this._event.latlng);
 
 481          * Toggles button class between following and active.
 
 483         _updateContainerStyle: function() {
 
 484             if (!this._container) {
 
 488             if (this._active && !this._event) {
 
 489                 // active but don't have a location yet
 
 490                 this._setClasses('requesting');
 
 491             } else if (this._isFollowing()) {
 
 492                 this._setClasses('following');
 
 493             } else if (this._active) {
 
 494                 this._setClasses('active');
 
 496                 this._cleanClasses();
 
 501          * Sets the CSS classes for the state.
 
 503         _setClasses: function(state) {
 
 504             if (state == 'requesting') {
 
 505                 L.DomUtil.removeClasses(this._container, "active following");
 
 506                 L.DomUtil.addClasses(this._container, "requesting");
 
 508                 L.DomUtil.removeClasses(this._icon, this.options.icon);
 
 509                 L.DomUtil.addClasses(this._icon, this.options.iconLoading);
 
 510             } else if (state == 'active') {
 
 511                 L.DomUtil.removeClasses(this._container, "requesting following");
 
 512                 L.DomUtil.addClasses(this._container, "active");
 
 514                 L.DomUtil.removeClasses(this._icon, this.options.iconLoading);
 
 515                 L.DomUtil.addClasses(this._icon, this.options.icon);
 
 516             } else if (state == 'following') {
 
 517                 L.DomUtil.removeClasses(this._container, "requesting");
 
 518                 L.DomUtil.addClasses(this._container, "active following");
 
 520                 L.DomUtil.removeClasses(this._icon, this.options.iconLoading);
 
 521                 L.DomUtil.addClasses(this._icon, this.options.icon);
 
 526          * Removes all classes from button.
 
 528         _cleanClasses: function() {
 
 529             L.DomUtil.removeClass(this._container, "requesting");
 
 530             L.DomUtil.removeClass(this._container, "active");
 
 531             L.DomUtil.removeClass(this._container, "following");
 
 533             L.DomUtil.removeClasses(this._icon, this.options.iconLoading);
 
 534             L.DomUtil.addClasses(this._icon, this.options.icon);
 
 538          * Reinitializes state variables.
 
 540         _resetVariables: function() {
 
 541             // whether locate is active or not
 
 542             this._active = false;
 
 544             // true if the control was clicked for the first time
 
 545             // we need this so we can pan and zoom once we have the location
 
 546             this._justClicked = false;
 
 548             // true if the user has panned the map after clicking the control
 
 549             this._userPanned = false;
 
 553     L.control.locate = function (options) {
 
 554         return new L.Control.Locate(options);
 
 558       // leaflet.js raises bug when trying to addClass / removeClass multiple classes at once
 
 559       // Let's create a wrapper on it which fixes it.
 
 560       var LDomUtilApplyClassesMethod = function(method, element, classNames) {
 
 561         classNames = classNames.split(' ');
 
 562         classNames.forEach(function(className) {
 
 563             L.DomUtil[method].call(this, element, className);
 
 567       L.DomUtil.addClasses = function(el, names) { LDomUtilApplyClassesMethod('addClass', el, names); };
 
 568       L.DomUtil.removeClasses = function(el, names) { LDomUtilApplyClassesMethod('removeClass', el, names); };
 
 571     return LocateControl;