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 LDomUtilApplyClassesMethod = function(method, element, classNames) {
30 classNames = classNames.split(' ');
31 classNames.forEach(function(className) {
32 L.DomUtil[method].call(this, element, className);
36 var addClasses = function(el, names) { LDomUtilApplyClassesMethod('addClass', el, names); };
37 var removeClasses = function(el, names) { LDomUtilApplyClassesMethod('removeClass', el, names); };
40 * Compatible with L.Circle but a true marker instead of a path
42 var LocationMarker = L.Marker.extend({
43 initialize: function (latlng, options) {
44 L.Util.setOptions(this, options);
45 this._latlng = latlng;
50 * Create a styled circle location marker
52 createIcon: function() {
53 var opt = this.options;
57 if (opt.color !== undefined) {
58 style += 'stroke:'+opt.color+';';
60 if (opt.weight !== undefined) {
61 style += 'stroke-width:'+opt.weight+';';
63 if (opt.fillColor !== undefined) {
64 style += 'fill:'+opt.fillColor+';';
66 if (opt.fillOpacity !== undefined) {
67 style += 'fill-opacity:'+opt.fillOpacity+';';
69 if (opt.opacity !== undefined) {
70 style += 'opacity:'+opt.opacity+';';
73 var icon = this._getIconSVG(opt, style);
75 this._locationIcon = L.divIcon({
76 className: icon.className,
78 iconSize: [icon.w,icon.h],
81 this.setIcon(this._locationIcon);
85 * Return the raw svg for the shape
87 * Split so can be easily overridden
89 _getIconSVG: function(options, style) {
90 var r = options.radius;
91 var w = options.weight;
94 var svg = '<svg xmlns="http://www.w3.org/2000/svg" width="'+s2+'" height="'+s2+'" version="1.1" viewBox="-'+s+' -'+s+' '+s2+' '+s2+'">' +
95 '<circle r="'+r+'" style="'+style+'" />' +
98 className: 'leaflet-control-locate-location',
105 setStyle: function(style) {
106 L.Util.setOptions(this, style);
111 var CompassMarker = LocationMarker.extend({
112 initialize: function (latlng, heading, options) {
113 L.Util.setOptions(this, options);
114 this._latlng = latlng;
115 this._heading = heading;
119 setHeading: function(heading) {
120 this._heading = heading;
124 * Create a styled arrow compass marker
126 _getIconSVG: function(options, style) {
127 var r = options.radius;
128 var w = (options.width + options.weight);
129 var h = (r+options.depth + options.weight)*2;
130 var path = 'M0,0 l'+(options.width/2)+','+options.depth+' l-'+(w)+',0 z';
131 var svgstyle = 'transform: rotate('+this._heading+'deg)';
132 var svg = '<svg xmlns="http://www.w3.org/2000/svg" width="'+(w)+'" height="'+h+'" version="1.1" viewBox="-'+(w/2)+' 0 '+w+' '+h+'" style="'+svgstyle+'">'+
133 '<path d="'+path+'" style="'+style+'" />'+
136 className: 'leaflet-control-locate-heading',
145 var LocateControl = L.Control.extend({
147 /** Position of the control */
149 /** The layer that the user's location should be drawn on. By default creates a new layer. */
152 * Automatically sets the map view (zoom and pan) to the user's location as it updates.
153 * While the map is following the user's location, the control is in the `following` state,
154 * which changes the style of the control and the circle marker.
157 * - false: never updates the map view when location changes.
158 * - 'once': set the view when the location is first determined
159 * - 'always': always updates the map view when location changes.
160 * The map view follows the user's location.
161 * - 'untilPan': like 'always', except stops updating the
162 * view if the user has manually panned the map.
163 * The map view follows the user's location until she pans.
164 * - 'untilPanOrZoom': (default) like 'always', except stops updating the
165 * view if the user has manually panned the map.
166 * The map view follows the user's location until she pans.
168 setView: 'untilPanOrZoom',
169 /** Keep the current map zoom level when setting the view and only pan. */
170 keepCurrentZoomLevel: false,
171 /** After activating the plugin by clicking on the icon, zoom to the selected zoom level, even when keepCurrentZoomLevel is true. Set to 'false' to disable this feature. */
172 initialZoomLevel: false,
174 * This callback can be used to override the viewport tracking
175 * This function should return a LatLngBounds object.
177 * For example to extend the viewport to ensure that a particular LatLng is visible:
179 * getLocationBounds: function(locationEvent) {
180 * return locationEvent.bounds.extend([-33.873085, 151.219273]);
183 getLocationBounds: function (locationEvent) {
184 return locationEvent.bounds;
186 /** Smooth pan and zoom to the location of the marker. Only works in Leaflet 1.0+. */
189 * The user location can be inside and outside the current view when the user clicks on the
190 * control that is already active. Both cases can be configures separately.
191 * Possible values are:
192 * - 'setView': zoom and pan to the current location
193 * - 'stop': stop locating and remove the location marker
196 /** What should happen if the user clicks on the control while the location is within the current view. */
198 /** What should happen if the user clicks on the control while the location is outside the current view. */
199 outOfView: 'setView',
201 * What should happen if the user clicks on the control while the location is within the current view
202 * and we could be following but are not. Defaults to a special value which inherits from 'inView';
204 inViewNotFollowing: 'inView',
207 * If set, save the map bounds just before centering to the user's
208 * location. When control is disabled, set the view back to the
209 * bounds that were saved.
211 returnToPrevBounds: false,
213 * Keep a cache of the location after the user deactivates the control. If set to false, the user has to wait
214 * until the locate API returns a new location before they see where they are again.
217 /** If set, a circle that shows the location accuracy is drawn. */
219 /** If set, the marker at the users' location is drawn. */
221 /** If set and supported then show the compass heading */
223 /** The class to be used to create the marker. For example L.CircleMarker or L.Marker */
224 markerClass: LocationMarker,
225 /** The class us be used to create the compass bearing arrow */
226 compassClass: CompassMarker,
227 /** Accuracy circle style properties. NOTE these styles should match the css animations styles */
229 className: 'leaflet-control-locate-circle',
231 fillColor: '#136AEC',
235 /** Inner marker style properties. Only works if your marker class supports `setStyle`. */
237 className: 'leaflet-control-locate-marker',
239 fillColor: '#2A93EE',
247 fillColor: '#2A93EE',
252 radius: 9, // How far is the arrow is from the center of of the marker
253 width: 9, // Width of the arrow
254 depth: 6 // Length of the arrow
257 * Changes to accuracy circle and inner marker while following.
258 * It is only necessary to provide the properties that should change.
260 followCircleStyle: {},
263 // fillColor: '#FFB000'
265 followCompassStyle: {},
266 /** The CSS class for the icon. For example fa-location-arrow or fa-map-marker */
267 icon: 'fa fa-map-marker',
268 iconLoading: 'fa fa-spinner fa-spin',
269 /** The element to be created for icons. For example span or i */
270 iconElementTag: 'span',
271 /** Padding around the accuracy circle. */
272 circlePadding: [0, 0],
273 /** Use metric units. */
276 * This callback can be used in case you would like to override button creation behavior.
277 * This is useful for DOM manipulation frameworks such as angular etc.
278 * This function should return an object with HtmlElement for the button (link property) and the icon (icon property).
280 createButtonCallback: function (container, options) {
281 var link = L.DomUtil.create('a', 'leaflet-bar-part leaflet-bar-part-single', container);
282 link.title = options.strings.title;
283 var icon = L.DomUtil.create(options.iconElementTag, options.icon, link);
284 return { link: link, icon: icon };
286 /** This event is called in case of any location error that is not a time out error. */
287 onLocationError: function(err, control) {
291 * This event is called when the user's location is outside the bounds set on the map.
292 * The event is called repeatedly when the location changes.
294 onLocationOutsideMapBounds: function(control) {
296 alert(control.options.strings.outsideMapBoundsMsg);
298 /** Display a pop-up when the user click on the inner marker. */
301 title: "Show me where I am",
302 metersUnit: "meters",
304 popup: "You are within {distance} {unit} from this point",
305 outsideMapBoundsMsg: "You seem located outside the boundaries of the map"
307 /** The default options passed to leaflets locate method. */
310 watch: true, // if you overwrite this, visualization cannot be updated
311 setView: false // have to set this to false because we have to
312 // do setView manually
316 initialize: function (options) {
317 // set default options if nothing is set (merge one step deep)
318 for (var i in options) {
319 if (typeof this.options[i] === 'object') {
320 L.extend(this.options[i], options[i]);
322 this.options[i] = options[i];
326 // extend the follow marker style and circle from the normal style
327 this.options.followMarkerStyle = L.extend({}, this.options.markerStyle, this.options.followMarkerStyle);
328 this.options.followCircleStyle = L.extend({}, this.options.circleStyle, this.options.followCircleStyle);
329 this.options.followCompassStyle = L.extend({}, this.options.compassStyle, this.options.followCompassStyle);
333 * Add control to map. Returns the container for the control.
335 onAdd: function (map) {
336 var container = L.DomUtil.create('div',
337 'leaflet-control-locate leaflet-bar leaflet-control');
339 this._layer = this.options.layer || new L.LayerGroup();
340 this._layer.addTo(map);
341 this._event = undefined;
342 this._compassHeading = null;
343 this._prevBounds = null;
345 var linkAndIcon = this.options.createButtonCallback(container, this.options);
346 this._link = linkAndIcon.link;
347 this._icon = linkAndIcon.icon;
350 .on(this._link, 'click', L.DomEvent.stopPropagation)
351 .on(this._link, 'click', L.DomEvent.preventDefault)
352 .on(this._link, 'click', this._onClick, this)
353 .on(this._link, 'dblclick', L.DomEvent.stopPropagation);
355 this._resetVariables();
357 this._map.on('unload', this._unload, this);
363 * This method is called when the user clicks on the control.
365 _onClick: function() {
366 this._justClicked = true;
367 var wasFollowing = this._isFollowing();
368 this._userPanned = false;
369 this._userZoomed = false;
371 if (this._active && !this._event) {
372 // click while requesting
374 } else if (this._active && this._event !== undefined) {
375 var behaviors = this.options.clickBehavior;
376 var behavior = behaviors.outOfView;
377 if (this._map.getBounds().contains(this._event.latlng)) {
378 behavior = wasFollowing ? behaviors.inView : behaviors.inViewNotFollowing;
381 // Allow inheriting from another behavior
382 if (behaviors[behavior]) {
383 behavior = behaviors[behavior];
392 if (this.options.returnToPrevBounds) {
393 var f = this.options.flyTo ? this._map.flyToBounds : this._map.fitBounds;
394 f.bind(this._map)(this._prevBounds);
399 if (this.options.returnToPrevBounds) {
400 this._prevBounds = this._map.getBounds();
405 this._updateContainerStyle();
410 * - activates the engine
411 * - draws the marker (if coordinates available)
417 this._drawMarker(this._map);
419 // if we already have a location but the user clicked on the control
420 if (this.options.setView) {
424 this._updateContainerStyle();
429 * - deactivates the engine
430 * - reinitializes the button
431 * - removes the marker
436 this._cleanClasses();
437 this._resetVariables();
439 this._removeMarker();
443 * Keep the control active but stop following the location
445 stopFollowing: function() {
446 this._userPanned = true;
447 this._updateContainerStyle();
452 * This method launches the location engine.
453 * It is called before the marker is updated,
454 * event if it does not mean that the event will be ready.
456 * Override it if you want to add more functionalities.
457 * It should set the this._active to true and do nothing if
458 * this._active is true.
460 _activate: function() {
462 this._map.locate(this.options.locateOptions);
465 // bind event listeners
466 this._map.on('locationfound', this._onLocationFound, this);
467 this._map.on('locationerror', this._onLocationError, this);
468 this._map.on('dragstart', this._onDrag, this);
469 this._map.on('zoomstart', this._onZoom, this);
470 this._map.on('zoomend', this._onZoomEnd, this);
471 if (this.options.showCompass) {
472 var oriAbs = 'ondeviceorientationabsolute' in window;
473 if (oriAbs || ('ondeviceorientation' in window)) {
475 var deviceorientation = function () {
476 L.DomEvent.on(window, oriAbs ? 'deviceorientationabsolute' : 'deviceorientation', _this._onDeviceOrientation, _this);
478 if (DeviceOrientationEvent && typeof DeviceOrientationEvent.requestPermission === 'function') {
479 DeviceOrientationEvent.requestPermission().then(function (permissionState) {
480 if (permissionState === 'granted') {
493 * Called to stop the location engine.
495 * Override it to shutdown any functionalities you added on start.
497 _deactivate: function() {
498 this._map.stopLocate();
499 this._active = false;
501 if (!this.options.cacheLocation) {
502 this._event = undefined;
505 // unbind event listeners
506 this._map.off('locationfound', this._onLocationFound, this);
507 this._map.off('locationerror', this._onLocationError, this);
508 this._map.off('dragstart', this._onDrag, this);
509 this._map.off('zoomstart', this._onZoom, this);
510 this._map.off('zoomend', this._onZoomEnd, this);
511 if (this.options.showCompass) {
512 this._compassHeading = null;
513 if ('ondeviceorientationabsolute' in window) {
514 L.DomEvent.off(window, 'deviceorientationabsolute', this._onDeviceOrientation, this);
515 } else if ('ondeviceorientation' in window) {
516 L.DomEvent.off(window, 'deviceorientation', this._onDeviceOrientation, this);
522 * Zoom (unless we should keep the zoom level) and an to the current view.
524 setView: function() {
526 if (this._isOutsideMapBounds()) {
527 this._event = undefined; // clear the current location so we can get back into the bounds
528 this.options.onLocationOutsideMapBounds(this);
530 if (this._justClicked && this.options.initialZoomLevel !== false) {
531 var f = this.options.flyTo ? this._map.flyTo : this._map.setView;
532 f.bind(this._map)([this._event.latitude, this._event.longitude], this.options.initialZoomLevel);
534 if (this.options.keepCurrentZoomLevel) {
535 var f = this.options.flyTo ? this._map.flyTo : this._map.panTo;
536 f.bind(this._map)([this._event.latitude, this._event.longitude]);
538 var f = this.options.flyTo ? this._map.flyToBounds : this._map.fitBounds;
539 // Ignore zoom events while setting the viewport as these would stop following
540 this._ignoreEvent = true;
541 f.bind(this._map)(this.options.getLocationBounds(this._event), {
542 padding: this.options.circlePadding,
543 maxZoom: this.options.locateOptions.maxZoom
545 L.Util.requestAnimFrame(function(){
546 // Wait until after the next animFrame because the flyTo can be async
547 this._ignoreEvent = false;
557 _drawCompass: function() {
562 var latlng = this._event.latlng;
564 if (this.options.showCompass && latlng && this._compassHeading !== null) {
565 var cStyle = this._isFollowing() ? this.options.followCompassStyle : this.options.compassStyle;
566 if (!this._compass) {
567 this._compass = new this.options.compassClass(latlng, this._compassHeading, cStyle).addTo(this._layer);
569 this._compass.setLatLng(latlng);
570 this._compass.setHeading(this._compassHeading);
571 // If the compassClass can be updated with setStyle, update it.
572 if (this._compass.setStyle) {
573 this._compass.setStyle(cStyle);
578 if (this._compass && (!this.options.showCompass || this._compassHeading === null)) {
579 this._compass.removeFrom(this._layer);
580 this._compass = null;
585 * Draw the marker and accuracy circle on the map.
587 * Uses the event retrieved from onLocationFound from the map.
589 _drawMarker: function() {
590 if (this._event.accuracy === undefined) {
591 this._event.accuracy = 0;
594 var radius = this._event.accuracy;
595 var latlng = this._event.latlng;
597 // circle with the radius of the location's accuracy
598 if (this.options.drawCircle) {
599 var style = this._isFollowing() ? this.options.followCircleStyle : this.options.circleStyle;
602 this._circle = L.circle(latlng, radius, style).addTo(this._layer);
604 this._circle.setLatLng(latlng).setRadius(radius).setStyle(style);
609 if (this.options.metric) {
610 distance = radius.toFixed(0);
611 unit = this.options.strings.metersUnit;
613 distance = (radius * 3.2808399).toFixed(0);
614 unit = this.options.strings.feetUnit;
617 // small inner marker
618 if (this.options.drawMarker) {
619 var mStyle = this._isFollowing() ? this.options.followMarkerStyle : this.options.markerStyle;
621 this._marker = new this.options.markerClass(latlng, mStyle).addTo(this._layer);
623 this._marker.setLatLng(latlng);
624 // If the markerClass can be updated with setStyle, update it.
625 if (this._marker.setStyle) {
626 this._marker.setStyle(mStyle);
633 var t = this.options.strings.popup;
634 function getPopupText() {
635 if (typeof t === 'string') {
636 return L.Util.template(t, {distance: distance, unit: unit});
637 } else if (typeof t === 'function') {
638 return t({distance: distance, unit: unit});
643 if (this.options.showPopup && t && this._marker) {
645 .bindPopup(getPopupText())
646 ._popup.setLatLng(latlng);
648 if (this.options.showPopup && t && this._compass) {
650 .bindPopup(getPopupText())
651 ._popup.setLatLng(latlng);
656 * Remove the marker from map.
658 _removeMarker: function() {
659 this._layer.clearLayers();
660 this._marker = undefined;
661 this._circle = undefined;
665 * Unload the plugin and all event listeners.
666 * Kind of the opposite of onAdd.
668 _unload: function() {
670 this._map.off('unload', this._unload, this);
674 * Sets the compass heading
676 _setCompassHeading: function(angle) {
677 if (!isNaN(parseFloat(angle)) && isFinite(angle)) {
678 angle = Math.round(angle);
680 this._compassHeading = angle;
681 L.Util.requestAnimFrame(this._drawCompass, this);
683 this._compassHeading = null;
688 * If the compass fails calibration just fail safely and remove the compass
690 _onCompassNeedsCalibration: function() {
691 this._setCompassHeading();
695 * Process and normalise compass events
697 _onDeviceOrientation: function(e) {
702 if (e.webkitCompassHeading) {
704 this._setCompassHeading(e.webkitCompassHeading);
705 } else if (e.absolute && e.alpha) {
707 this._setCompassHeading(360 - e.alpha)
712 * Calls deactivate and dispatches an error.
714 _onLocationError: function(err) {
715 // ignore time out error if the location is watched
716 if (err.code == 3 && this.options.locateOptions.watch) {
721 this.options.onLocationError(err, this);
725 * Stores the received event and updates the marker.
727 _onLocationFound: function(e) {
728 // no need to do anything if the location has not changed
730 (this._event.latlng.lat === e.latlng.lat &&
731 this._event.latlng.lng === e.latlng.lng &&
732 this._event.accuracy === e.accuracy)) {
737 // we may have a stray event
744 this._updateContainerStyle();
746 switch (this.options.setView) {
748 if (this._justClicked) {
753 if (!this._userPanned) {
757 case 'untilPanOrZoom':
758 if (!this._userPanned && !this._userZoomed) {
766 // don't set the view
770 this._justClicked = false;
774 * When the user drags. Need a separate event so we can bind and unbind event listeners.
776 _onDrag: function() {
777 // only react to drags once we have a location
778 if (this._event && !this._ignoreEvent) {
779 this._userPanned = true;
780 this._updateContainerStyle();
786 * When the user zooms. Need a separate event so we can bind and unbind event listeners.
788 _onZoom: function() {
789 // only react to drags once we have a location
790 if (this._event && !this._ignoreEvent) {
791 this._userZoomed = true;
792 this._updateContainerStyle();
798 * After a zoom ends update the compass and handle sideways zooms
800 _onZoomEnd: function() {
805 if (this._event && !this._ignoreEvent) {
806 // If we have zoomed in and out and ended up sideways treat it as a pan
807 if (this._marker && !this._map.getBounds().pad(-.3).contains(this._marker.getLatLng())) {
808 this._userPanned = true;
809 this._updateContainerStyle();
816 * Compute whether the map is following the user location with pan and zoom.
818 _isFollowing: function() {
823 if (this.options.setView === 'always') {
825 } else if (this.options.setView === 'untilPan') {
826 return !this._userPanned;
827 } else if (this.options.setView === 'untilPanOrZoom') {
828 return !this._userPanned && !this._userZoomed;
833 * Check if location is in map bounds
835 _isOutsideMapBounds: function() {
836 if (this._event === undefined) {
839 return this._map.options.maxBounds &&
840 !this._map.options.maxBounds.contains(this._event.latlng);
844 * Toggles button class between following and active.
846 _updateContainerStyle: function() {
847 if (!this._container) {
851 if (this._active && !this._event) {
852 // active but don't have a location yet
853 this._setClasses('requesting');
854 } else if (this._isFollowing()) {
855 this._setClasses('following');
856 } else if (this._active) {
857 this._setClasses('active');
859 this._cleanClasses();
864 * Sets the CSS classes for the state.
866 _setClasses: function(state) {
867 if (state == 'requesting') {
868 removeClasses(this._container, "active following");
869 addClasses(this._container, "requesting");
871 removeClasses(this._icon, this.options.icon);
872 addClasses(this._icon, this.options.iconLoading);
873 } else if (state == 'active') {
874 removeClasses(this._container, "requesting following");
875 addClasses(this._container, "active");
877 removeClasses(this._icon, this.options.iconLoading);
878 addClasses(this._icon, this.options.icon);
879 } else if (state == 'following') {
880 removeClasses(this._container, "requesting");
881 addClasses(this._container, "active following");
883 removeClasses(this._icon, this.options.iconLoading);
884 addClasses(this._icon, this.options.icon);
889 * Removes all classes from button.
891 _cleanClasses: function() {
892 L.DomUtil.removeClass(this._container, "requesting");
893 L.DomUtil.removeClass(this._container, "active");
894 L.DomUtil.removeClass(this._container, "following");
896 removeClasses(this._icon, this.options.iconLoading);
897 addClasses(this._icon, this.options.icon);
901 * Reinitializes state variables.
903 _resetVariables: function() {
904 // whether locate is active or not
905 this._active = false;
907 // true if the control was clicked for the first time
908 // we need this so we can pan and zoom once we have the location
909 this._justClicked = false;
911 // true if the user has panned the map after clicking the control
912 this._userPanned = false;
914 // true if the user has zoomed the map after clicking the control
915 this._userZoomed = false;
919 L.control.locate = function (options) {
920 return new L.Control.Locate(options);
923 return LocateControl;