Draw ring (not circle) in Google Maps API

后端 未结 2 1748
庸人自扰
庸人自扰 2020-12-10 23:39

I would like to draw a ring 20km thick with empty 5km circle inside. I dont how how to do it. I believe it is possible.

One simple solution could be to substract one

2条回答
  •  失恋的感觉
    2020-12-10 23:53

    Here's a slightly modified version of geocodezip's answer using the geometry library:

    function getCirclePoints(center, radius, numPoints, clockwise) {
        var points = [];
        for (var i = 0; i < numPoints; ++i) {
            var angle = i * 360 / numPoints;
            if (!clockwise) {
                angle = 360 - angle;
            }
    
            // the maps API provides geometrical computations
            // just make sure you load the required library (libraries=geometry)
            var p = google.maps.geometry.spherical.computeOffset(center, radius, angle);
            points.push(p);
        }
    
        // 'close' the polygon
        points.push(points[0]);
        return points;
    }
    

    You can use it like this:

    new google.maps.Polygon({
        paths: [
            getCirclePoints(yourCenter, outerRadius, numPoints, true),
            getCirclePoints(yourCenter, innerRadius, numPoints, false)
        ],
        /* other polygon options */
     });
    

    Edit:

    The geometry API can be added in Node like this (thanks to Gil Epshtain):

    require('google-maps-api')(GOOGLE_API_KEY, ['geometry'])
    

    By the time I was writing this, I used plain old JavaScript inclusion in HTML:

    
    
                                     
                  
提交回复
热议问题