【问题标题】:How to show data based on a location如何根据位置显示数据
【发布时间】:2019-12-10 13:27:39
【问题描述】:

我想构建一个用户发布某种文本的应用程序,任何人都可以访问“博客帖子”,但我只希望 X KM 半径内的人阅读它(将其显示在该范围内的用户的提要上半径)最好的方法是什么?提前致谢。

尝试将坐标上传到数据库,但不知道如何从那里继续,程序检查半径内的位置,然后显示博文

【问题讨论】:

  • 我认为使用 parse-server 会很容易,在我的宠物项目中有类似的东西。
  • 你能举个例子吗?
  • 嗯,它确实需要的不仅仅是 SO 示例。但他们有很好的文档和 SDK 可供使用。

标签: php node.js reactjs


【解决方案1】:

在 Node.js 中,我建议使用带有 geofirex 的 firestore,这样您就可以在一定范围内进行搜索,并且借助 firebase 功能,您甚至可以创建实时查询。

我制作了this web app,它只显示半径内的点。

存储和读取值的主要函数如下:

阅读

const DEFAULT_POINT = geo.point(32.520666, -117.021315);
const opinions = db.collection('opinions');

const opinionsSubscribe = () => {
  // Here we check opinions within 22km of radius
  // 'position' is the field where we store our geofirex point
  const query = reports.within(DEFAULT_POINT, 22, 'position');
  // This pipe just parse the data to geojson to make it easy to render directly on a map (tested with google and esri)
  const obs = query.pipe(toGeoJSON('position', true));
  obs.subscribe((geoj) => {
    console.log('reports changed');
    mainReportsData.geo = geoj;
    io.emit('reports', {
      metric: mainReportsData.geo,
    });
  });
};

opinionsSubscribe();

写作


const storeOpinion = (uid, info, res) => {
  const {
    address,
    latitude,
    longitude,
    opinion,
  } = info;
  const lat = parseFloat(latitude);
  const lon = parseFloat(longitude);
  const timestamp = admin.firestore.FieldValue.serverTimestamp();
  // We use default firestore add function, so we need to extract the data from geofirex GeoPoint function
  const { data } = geo.point(lat, lon);
  // The attribute that stores entry location
  const position = {
    geopoint: new admin.firestore.GeoPoint(lat, lon),
    geohash: data.geohash,
  };
  opinions.add({
    address,
    timestamp,
    user: uid,
    position,
  })
    .then(() => {
      res.send({ data: 'success' });
    })
    .catch(() => {
      res.send({ data: 'error' });
    });
};

您可以从this project探索代码,几乎是相同的代码,请随时提出任何问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2012-08-21
    • 1970-01-01
    • 2023-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多