dow58115 2019-08-01 22:18
浏览 178
已采纳

too long

I want to build an app where a user posts some sort of text, the "blogpost" is accesible to anyone, but I only want people within a X KM radius to read it (display it on the feeds of users within that radius) What is the best way to do this? Thanks in advance.

Tried uploading the coordinates to a database, but not sure how to move on from there where the program checks for locations within the radius and then displays the blogpost

  • 写回答

1条回答 默认 最新

  • douyun6399 2019-08-01 23:29
    关注

    In Node.js, I would recommend using firestore with geofirex, so you can search within a certain radius, and thanks to firebase functions, you can even create real-time queries.

    I made this web app that shows only points within a radius.

    The main functions to store and read values are like this:

    Reading

    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();
    

    Writing

    
    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' });
        });
    };
    

    You can explore the code from this project, is almost the same code, feel free to ask any question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?