smithmicro / mapbox-gl-circle

A *google.maps.Circle* replacement for Mapbox GL JS
https://smithmicro.github.io/mapbox-gl-circle/
ISC License
55 stars 26 forks source link
google-maps mapbox-gl turfjs

Spherical-Cap "Native Circle" for Mapbox GL JS

Lint and Build Publish Pre-Release Publish Release NPM Version

This project uses Turf.js to create a google.maps.Circle replacement, as a Mapbox GL JS compatible GeoJSON object. Allowing the developer to define a circle using center coordinates and radius (in meters). And, optionally, enabling interactive editing via draggable center/radius handles. Just like the Google original!

Getting Started

Include mapbox-gl-circle.min.js in the <head> of your HTML file to add the MapboxCircle object to global scope:

<script src='https://npmcdn.com/mapbox-gl-circle/dist/mapbox-gl-circle.min.js'></script>

Or even better, fashionably importing it using a module bundler:

npm install --save mapbox-gl-circle
const MapboxCircle = require('mapbox-gl-circle');
// or "import MapboxCircle from 'mapbox-gl-circle';"

Usage

MapboxCircle

A google.maps.Circle replacement for Mapbox GL JS, rendering a "spherical cap" on top of the world.

Parameters

Examples

var myCircle = new MapboxCircle({lat: 39.984, lng: -75.343}, 25000, {
        editable: true,
        minRadius: 1500,
        fillColor: '#29AB87'
    }).addTo(myMapboxGlMap);

myCircle.on('centerchanged', function (circleObj) {
        console.log('New center:', circleObj.getCenter());
    });
myCircle.once('radiuschanged', function (circleObj) {
        console.log('New radius (once!):', circleObj.getRadius());
    });
myCircle.on('click', function (mapMouseEvent) {
        console.log('Click:', mapMouseEvent.point);
    });
myCircle.on('contextmenu', function (mapMouseEvent) {
        console.log('Right-click:', mapMouseEvent.lngLat);
    });

constructor

Parameters

on

Subscribe to circle event.

Parameters

Returns MapboxCircle

once

Alias for registering event listener with onlyOnce=true, see #on.

Parameters

Returns MapboxCircle

off

Unsubscribe to circle event.

Parameters

Returns MapboxCircle

addTo

Parameters

Returns MapboxCircle

remove

Remove source data, layers and listeners from map.

Returns MapboxCircle

getCenter

Returns {lat: number, lng: number} Circle center position

setCenter

Parameters

Returns MapboxCircle

getRadius

Returns number Current radius, in meters

setRadius

Parameters

Returns MapboxCircle

getBounds

Returns {sw: {lat: number, lng: number}, ne: {lat: number, lng: number}} Southwestern/northeastern bounds

Development

Install Dependencies

npm install

Run Locally

npm start

Build Development Bundle

npm run browserify

Build Distributable Package

npm pack

Update README API Documentation

npm run docs

Changelog

v. 1.6.7

v. 1.6.6

v. 1.6.5

v. 1.6.4

v. 1.6.3

v. 1.6.2

v. 1.6.1

v. 1.6.0

v. 1.5.2

v. 1.5.1

v. 1.5.0

v. 1.4.3

v. 1.4.2

v. 1.4.1

v. 1.4.0

v. 1.3.0

v. 1.2.5

v. 1.2.4

v. 1.2.3

v. 1.2.2

v. 1.2.1

v. 1.2.0

v. 1.1.0

Updated circle from Mapbox bl.ocks.org sample.

Now provides handles for modifying position/radius. Seems to also do better performance wise.

v. 1.0.0

The initial 1.0.0 release is a modified version of the Draw-Circle.zip archive we got from Mapbox.

Live demo of the original can be found here: https://www.mapbox.com/labs/draw-circle/