如何将谷歌地图中的圆形转换为GeoJSON?

5
1个回答

17

Google Maps在几何库中配备了一组方便的球形函数,使我们可以轻松实现此功能。具体来说,我们需要 computeOffset 函数:

返回从指定朝向(顺时针从北方表示的度数)的原点移动距离后得到的LatLng。

我们有原点(圆的中心)和距离(圆的半径),因此我们只需要根据需要的边数计算一组点的朝向即可。

function generateGeoJSONCircle(center, radius, numSides){

  var points = [],
      degreeStep = 360 / numSides;

  for(var i = 0; i < numSides; i++){
    var gpos = google.maps.geometry.spherical.computeOffset(center, radius, degreeStep * i);
    points.push([gpos.lng(), gpos.lat()]);
  };

  // Duplicate the last point to close the geojson ring
  points.push(points[0]);

  return {
    type: 'Polygon',
    coordinates: [ points ]
  };
}

几何库不是默认包含的。您必须通过库参数特别请求它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接