最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I convert a google maps circle into GeoJSON? - Stack Overflow

programmeradmin3浏览0评论

I need to convert a google maps Circle into GeoJSON. GeoJSON doesn't support circles so I want to generate an N-sided polygon. I found a nice method for generating a regular N-sided polygon but that requires the radius to be defined in the coordinate system while the radius of a circle in Google Maps is defined in meters.

I need to convert a google maps Circle into GeoJSON. GeoJSON doesn't support circles so I want to generate an N-sided polygon. I found a nice method for generating a regular N-sided polygon but that requires the radius to be defined in the coordinate system while the radius of a circle in Google Maps is defined in meters.

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Mar 6, 2014 at 16:14 user879121user879121
Add a ment  | 

1 Answer 1

Reset to default 17

Google Maps has a handy set of spherical functions in the geometry library that makes this really easy for us. Specifically, we want the puteOffset function:

Returns the LatLng resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north).

We have the origin (the center of the circle) and a distance (the radius of the circle), so we just need to calculate a set of headings for the points based on how many sides we want.

function generateGeoJSONCircle(center, radius, numSides){

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

  for(var i = 0; i < numSides; i++){
    var gpos = google.maps.geometry.spherical.puteOffset(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 ]
  };
}

The geometry library is not included by default. You must specifically ask for it via the libraries parameter.

发布评论

评论列表(0)

  1. 暂无评论