java - 如何在 Firebase 中实时存储位置?

标签 java android firebase geolocation

我正在使用带有 Firebase 的 Android Studio。我已将我的项目链接到数据库并完成了用于登录和注册的用户界面。用户现在可以注册,我可以在 Firebase 中看到。用户登录后,他们可以看到 MapsActivity(谷歌地图,他们可以在其中跟踪自己的位置,但该位置并未存储​​在任何地方,我观看了有关跟踪自己位置的 YouTube 教程)

问题:我真正想要的是,当用户注册时,他们的位置也被发送到数据库中(所以每个用户都有一个称为位置的属性),这样当用户登录时,可以检索到他/她自己的位置跟踪位置。

基本上,Firebase 应该在用户注册时存储位置,并在用户移动时不断更新它。用户登录后,应检索位置(以便用户可以跟踪当前位置)。同样,当用户移动时,应更新 Firebase 中的位置。

我尝试在网上查找,但似乎找不到解决方案。

最佳答案

您可以使用 LocationListener .这是一个简单的例子:

 LocationListener locationListener = new android.location.LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            // Push your location to FireBase
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };

然后你通过你的位置监听器到位置管理器
LocationManager locationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

这将从 GPRS 和 NetworkProvider 监听位置变化

要将更改推送到 Firebase,您可以通过以下方式进行
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference myRef = database.getReference();

发布到数据库时,您必须唯一标识一行,就像在普通数据库中一样(即 ID、UUID),因此您必须生成一个并存储到您 SharedPreferences例如。
public void postToDatabase(UserLocation location) {
    DatabaseReference usersRef = myRef.child(someUUID);
    usersRef.setValue(location);
}

要监听更改,您必须实现 ValueEventListener .一个例子,但必须适应您的需求
ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                UserLocation location = new UserLocation();
                location.setLongitude((String) snapshot.child("longitude").getValue());
                location.setLatitude((String) snapshot.child("latitute").getValue());
               //Dispatch your changes in application, i.e with and EventBus
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

关于java - 如何在 Firebase 中实时存储位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44670356/

相关文章:

java - 如何通过在android中的.java文件中提供imgsrc来加载imageview

java - 如何从 SpringBoot 应用程序发出自定义指标并在 PCF Autoscaler 中使用它

android - Twitter4J android查询搜索不返回所有推文

android - 如何从 Activity 迁移到 fragment

firebase - 无法在 Flutter 应用程序中通过用户 ID 获取特定用户文档

javascript - 如何获取 firebase 数据库的主要对象?

java - 如何在 Jboss 7 中部署 jboss seam 2.1.2

java - Java 中的 Play 框架异步处理和阻塞 I/O

android - 如何自动更新android :versionName?

javascript - React Fetch Google Cloud Function http cors req.method 选项,POST