【问题标题】:Handling geocoding in Firebase?在 Firebase 中处理地理编码?
【发布时间】:2014-01-18 20:28:35
【问题描述】:

有没有人尝试在 Firebase 中存储和/或搜索地理编码(例如纬度/经度)?这是 MongoDB 中内置的功能,因为我正在考虑将 Firebase 用作我们的后端,所以我需要知道如何在 Firebase 中处理这种情况。

谢谢!

【问题讨论】:

    标签: geocoding firebase geofire


    【解决方案1】:

    Firebase 的人们最近开源了一个库,允许您在 Firebase 中存储和查询位置数据。简而言之,这很容易实现,因为 Firebase 中的所有键都是字符串,而且 Firebase 支持 startAt()endAt() 查询,允许您对地理哈希和边界框进行适当的窗口化。

    有关实施细节和用法,请查看 GeoFire 上的 live demossource code 和他们的 blog post

    【讨论】:

      【解决方案2】:

      嘿,我刚刚使用 Firebase 和 GeoFire 构建了一个实时谷歌地图。 GeoFire 非常酷且易于使用。它允许您使用 lon lat 和 radius 进行查询。它返回一个键,您可以使用它来查询您的 firebase 数据库。您在创建 geoFire 对象时将密钥设置为您想要的任何内容。它通常是一个 ref,您可以使用它来获取与该距离关联的对象。

      这里是 geoFire 的链接: https://github.com/firebase/geofire-js

      这是一个示例用例:

      你有一个 lon lat,你用导航器得到的:

      var lon = '123.1232';
      var lat = '-123.756';
      var user = {
          name: 'test user',
          longitude: lon,
          latitude: lat
      }
      usersRef.push(user).then(function(response) {
          var key = response.key;
          var coords = [lon, lat];
          geoFire.set(key, coords, function(success){
               console.log('User and geofire object has been created');
          });
      })
      

      现在您可以使用以下方法查询用户:

      // Set your current lon lat and radius
      var geoQuery = geoFire.query({
          center: [latitude, longitude],
          radius: radiusKm
      });
      geoQuery.on('key_entered', function(key, location, distance) {
          // You can now get the user using the key
          var user = firebaseRefUrl + '/' + key;
      
          // Here you can create an array of users that you can bind to a   scope in the controller
      });
      

      如果您使用的是谷歌地图。我建议您使用 angular-google-maps。 它是一个非常酷的谷歌地图指令,它接受一系列标记和圆圈。因此,当控制器中的 $scope.markers 或 $scope.circles 发生变化时,它会自动应用到地图上,而无需任何杂乱的代码。他们有很好的文档。

      这是一个链接: http://angular-ui.github.io/angular-google-maps/

      【讨论】:

        猜你喜欢
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        • 1970-01-01
        • 1970-01-01
        • 2021-09-13
        • 2011-05-17
        • 1970-01-01
        相关资源
        最近更新 更多