使用谷歌s2库 - 找到圆圈内某个级别的所有s2单元格,给定纬度/经度和半径,单位为英里/公里

时间:2016-07-20 04:18:52

标签: google-maps google-api

我应该使用什么S2Region以及如何使用它来获取圆圈内的所有单元格,使用google的s2库给出纬度,经度和半径(英里/公里)?

S2Region region = ?
S2RegionCoverer coverer = new S2RegionCoverer();
coverer.setMinLevel(17);
coverer.setMaxCells(17);
S2CellUnion covering = coverer.getCovering(region_cap);

由于

2 个答案:

答案 0 :(得分:1)

这是来自npm包s2geometry-node的C ++示例。代码是从viewfinder / viewfinder.cc

复制的
const double kEarthCircumferenceMeters = 1000 * 40075.017;

double EarthMetersToRadians(double meters) {
  return (2 * M_PI) * (meters / kEarthCircumferenceMeters);
}

string CellToString(const S2CellId& id) {
  return StringPrintf("%d:%s", id.level(), id.ToToken().c_str());
}

// Generates a list of cells at the target s2 cell levels which cover
// a cap of radius 'radius_meters' with center at lat & lng.
vector<string> SearchCells(double lat, double lng, double radius_meters,
                           int min_level, int max_level) {
  const double radius_radians = EarthMetersToRadians(radius_meters);
  const S2Cap region = S2Cap::FromAxisHeight(
      S2LatLng::FromDegrees(lat, lng).Normalized().ToPoint(),
      (radius_radians * radius_radians) / 2);
  S2RegionCoverer coverer;
  coverer.set_min_level(min_level);
  coverer.set_max_level(max_level);

  vector<S2CellId> covering;
  coverer.GetCovering(region, &covering);
  vector<string> v(covering.size());
  for (size_t i = 0; i < covering.size(); ++i) {
    v[i] = CellToString(covering[i]);
  }
  return v;
}

答案 1 :(得分:0)

我遇到了同样的任务,并使用S2RegionCoverer.getCovering(S2Region region, ArrayList<S2CellId> covering)方法解决了这个问题。

S2RegionCoverer.getCovering(S2Region region)文档中描述了获取不同级别单元格的原因:

/**
   * Return a normalized cell union that covers the given region and satisfies
   * the restrictions *EXCEPT* for min_level() and level_mod(). These criteria
   * cannot be satisfied using a cell union because cell unions are
   * automatically normalized by replacing four child cells with their parent
   * whenever possible. (Note that the list of cell ids passed to the cell union
   * constructor does in fact satisfy all the given restrictions.)
   */