mapbox-gl-circle-ts v1.0.5
Spherical-Cap "Native Circle" for Mapbox GL JS in Typescript
This project uses Turf.js to create a google.maps.Circle replacement, as a Mapbox GL JS compatible GeoJSON object for Typescript.
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
npm install --save mapbox-gl-circleconst 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
centerradiusoptions
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
center({lat: number, lng: number} | [number, number]) Circle center as an object or[lng, lat]coordinatesradiusnumber Meter radiusoptionsObject?options.editableboolean? Enable handles for changing center and radius (optional, defaultfalse)options.minRadiusnumber? Minimum radius on user interaction (optional, default10)options.maxRadiusnumber? Maximum radius on user interaction (optional, default1100000)options.strokeColorstring? Stroke color (optional, default'#000000')options.strokeWeightnumber? Stroke weight (optional, default0.5)options.strokeOpacitynumber? Stroke opacity (optional, default0.75)options.fillColorstring? Fill color (optional, default'#FB6A4A')options.fillOpacitynumber? Fill opacity (optional, default0.25)options.refineStrokeboolean? Adjust circle polygon precision based on radius and zoom (i.e. prettier circles at the expense of performance) (optional, defaultfalse)options.propertiesObject? Property metadata for Mapbox GL JS circle object (optional, default{})
on
Subscribe to circle event.
Parameters
eventstring Event name;click,contextmenu,centerchangedorradiuschangedfnFunction Event handler, invoked with the target circle as first argument on 'centerchanged' and 'radiuschanged', or a MapMouseEvent on 'click' and 'contextmenu' eventsonlyOnceboolean? Remove handler after first call (optional, defaultfalse)
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
mapmapboxgl.Map Target map for adding and initializing circle Mapbox GL layers/data/listeners.beforestring? Layer ID to insert the circle layers before; explicitly passnullto get the circle assets appended at the end of map-layers array (optional, default'waterway-label')
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
newRadiusnumber Meter radius
Returns MapboxCircle
getBounds
Returns {sw: {lat: number, lng: number}, ne: {lat: number, lng: number}} Southwestern/northeastern bounds
Development
Install Dependencies
npm installBuild Development Bundle
npm run browserifyChangelog
v. 1.0.1
Adds new build scripts and index.html with usage example.
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/