GithubHelp home page GithubHelp logo

bradparks / leaflet-locatecontrol Goto Github PK

View Code? Open in Web Editor NEW

This project forked from domoritz/leaflet-locatecontrol

0.0 2.0 0.0 2.99 MB

A leaflet control to geolocate the user.

Home Page: http://domoritz.github.io/leaflet-locatecontrol/demo/

License: MIT License

leaflet-locatecontrol's Introduction

Leaflet.Locate

A useful control to geolocate the user with many options.

Tested with Leaflet 0.7 in Firefox, Webkit and mobile Webkit.

Demo

Check out the demo at http://domoritz.github.io/leaflet-locatecontrol/demo/

Usage

Minimal set up:

Add the JavaScript and CSS files

Download and include the JavaScript and CSS files.

For testing purposes and development, you can use the latest version directly from my repository using rawgithub. However, don't do this in production environments! For production environments, use the mapbox CDN.

<link rel="stylesheet" href="//rawgithub.com/domoritz/leaflet-locatecontrol/gh-pages/src/L.Control.Locate.css" />
<!--[if lt IE 9]>
    <link rel="stylesheet" href="//rawgithub.com/domoritz/leaflet-locatecontrol/gh-pages/src/L.Control.Locate.ie.css"/>
<![endif]-->

<script src="//rawgithub.com/domoritz/leaflet-locatecontrol/gh-pages/src/L.Control.Locate.js" ></script>

Add the following snippet to your map initialization:

L.control.locate().addTo(map);

Install using bower

To install the locate control with Bower, just run bower install leaflet.locatecontrol.

Possible options

The locate controls inherits options from Leaflet Controls.

L.control.locate({
	position: 'topleft',  // set the location of the control
    drawCircle: true,  // controls whether a circle is drawn that shows the uncertainty about the location
    follow: false,  // follow the user's location
    setView: true, // automatically sets the map view to the user's location, enabled if `follow` is true
    keepCurrentZoomLevel: false, // keep the current map zoom level when displaying the user's location. (if `false`, use maxZoom)
    stopFollowingOnDrag: false, // stop following when the map is dragged if `follow` is true (deprecated, see below)
    remainActive: false, // if true locate control remains active on click even if the user's location is in view.
    markerClass: L.circleMarker, // L.circleMarker or L.marker
    circleStyle: {},  // change the style of the circle around the user's location
    markerStyle: {},
    followCircleStyle: {},  // set difference for the style of the circle around the user's location while following
    followMarkerStyle: {},
    icon: 'icon-location',  // `icon-location` or `icon-direction`
    iconLoading: 'icon-spinner  animate-spin',  // class for loading icon
    circlePadding: [0, 0], // padding around accuracy circle, value is passed to setBounds
    metric: true,  // use metric or imperial units
    onLocationError: function(err) {alert(err.message)},  // define an error callback function
    onLocationOutsideMapBounds:  function(context) { // called when outside map boundaries
            alert(context.options.strings.outsideMapBoundsMsg);
    },
    showPopup: true, // display a popup when the user click on the inner marker
    strings: {
        title: "Show me where I am",  // title of the locate control
        popup: "You are within {distance} {unit} from this point",  // text to appear if user clicks on circle
        outsideMapBoundsMsg: "You seem located outside the boundaries of the map" // default message for onLocationOutsideMapBounds
    }
    locateOptions: {}  // define location options e.g enableHighAccuracy: true or maxZoom: 10
}).addTo(map);

Methods

You can call locate() or stopLocate() on the locate control object to set the location of page load for example.

// create control and add to map
var lc = L.control.locate().addTo(map);

// request location update and set location
lc.locate();

You can also use the helper functions to automatically stop following when the map is panned. See the example below.

var lc = L.control.locate().addTo(map);
map.on('dragstart', lc.stopFollowing);

Alternatively, you can unload events when not following to avoid unnecessary events.

map.on('startfollowing', function() {
    map.on('dragstart', lc.stopFollowing);
}).on('stopfollowing', function() {
    map.off('dragstart', lc.stopFollowing);
});

Events

The locate control fires startfollowing and stopfollowing on the map object and passes self as data.

FAQ

How do I set the maximum zoom level?

Set the maxZoom in locateOptions (keepCurrentZoomLevel must not be set to true).

map.addControl(L.control.locate({
       locateOptions: {
               maxZoom: 10
}}));

Screenshot

screenshot

Users

Sites that use this locate control:

Developers

Run the demo locally with python -m SimpleHTTPServer and then open http://0.0.0.0:8000/demo.

Thanks

To all contributors and issue reporters.

License

MIT

leaflet-locatecontrol's People

Contributors

bmcbride avatar cebe avatar domoritz avatar fnicollet avatar gabrielflorit avatar gitare avatar gmccrackin avatar jeromecaffaro avatar jtomaszewski avatar karlguillotte avatar mobilesam avatar omjokine avatar plaxdan avatar radicalbiscuit avatar rhubarbrhubarb avatar tmcw avatar uintaam avatar webot avatar

Watchers

 avatar  avatar

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.