簡體   English   中英

如何將Google地圖圈轉換為GeoJSON?

[英]How can I convert a google maps circle into GeoJSON?

我需要將google maps Circle轉換為GeoJSON。 GeoJSON不支持圓,所以我想生成一個N邊多邊形。 我發現了一種生成常規N邊多邊形的好方法,但是需要在坐標系中定義半徑,而Google地圖中圓的半徑以米為單位。

谷歌地圖在幾何庫中有一套方便的球形函數 ,這對我們來說非常簡單。 具體來說,我們需要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 ]
  };
}

默認情況下不包含幾何庫 您必須通過libraries參數專門詢問它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM