Created
June 12, 2017 14:31
-
-
Save jhned/efac6cf9f35d77598e5ebb8155fc5887 to your computer and use it in GitHub Desktop.
Add a circle overlay polygon in Mapbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var createGeoJSONCircle = function(center, radiusInKm, points) { | |
if(!points) points = 64; | |
var coords = { | |
latitude: center[1], | |
longitude: center[0] | |
}; | |
var km = radiusInKm; | |
var ret = []; | |
var distanceX = km/(111.320*Math.cos(coords.latitude*Math.PI/180)); | |
var distanceY = km/110.574; | |
var theta, x, y; | |
for(var i=0; i<points; i++) { | |
theta = (i/points)*(2*Math.PI); | |
x = distanceX*Math.cos(theta); | |
y = distanceY*Math.sin(theta); | |
ret.push([coords.longitude+x, coords.latitude+y]); | |
} | |
ret.push(ret[0]); | |
return { | |
"type": "geojson", | |
"data": { | |
"type": "FeatureCollection", | |
"features": [{ | |
"type": "Feature", | |
"geometry": { | |
"type": "Polygon", | |
"coordinates": [ret] | |
} | |
}] | |
} | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map.addSource("polygon", createGeoJSONCircle([-93.6248586, 41.58527859], 0.5)); | |
map.addLayer({ | |
"id": "polygon", | |
"type": "fill", | |
"source": "polygon", | |
"layout": {}, | |
"paint": { | |
"fill-color": "blue", | |
"fill-opacity": 0.6 | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://stackoverflow.com/questions/37599561/drawing-a-circle-with-the-radius-in-miles-meters-with-mapbox-gl-js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment