Drawing a circle with the radius in miles/meters with Mapbox GL JS

后端 未结 4 1612
自闭症患者
自闭症患者 2020-12-08 00:15

I\'m in the process of converting a map from using mapbox.js to mapbox-gl.js, and am having trouble drawing a circle that uses miles or met

4条回答
  •  甜味超标
    2020-12-08 01:00

    I've solved this problem for my use cases by using a GeoJSON polygon. It's not strictly a circle but by increasing the number of sides on the polygon you can get pretty close.

    The added benefit to this method is that it will correctly change its pitch, size, bearing, etc with the map automatically.

    Here is the function to generate the GeoJSON Polygon

    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

    You can use it like this:

    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
        }
    });
    

    If you need to update the circle you created later you can do it like this (note the need to grab the data property to pass to setData):

    map.getSource('polygon').setData(createGeoJSONCircle([-93.6248586, 41.58527859], 1).data);
    

    And the output looks like this:

提交回复
热议问题