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.

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

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?