【问题标题】:OnLocationChange send coordinates to FirebaseOnLocationChange 将坐标发送到 Firebase
【发布时间】:2016-12-29 13:27:19
【问题描述】:

我正在制作一个 Capstone 项目,我急需帮助,这几天被困在这上面,我想在用户单击按钮时向 Firebase 发送位置更新。更新设置为在位置更改时每 3 秒发送一次。但问题是它只会在按下按钮后发送坐标,而不是持续发送位置变化的更新。这是我的代码

Start = (Button) findViewById(R.id.btnLoc);
Stop = (Button) findViewById(R.id.btnStopSend);

    mRef = new Firebase ("FIREBASE URL");

    Start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                Firebase busCoords = mRef.child("Location");
                busCoords.setValue(location.getLatitude()+ ",   "+location.getLongitude());
            }

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

            @Override
            public void onProviderEnabled(String s) {
            }

            @Override
            public void onProviderDisabled(String s) {
            }
        };

            locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

            //noinspection MissingPermission
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,1000,listener);
        }
    });

    Stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(locationManager != null){
                //noinspection MissingPermission
                locationManager.removeUpdates(listener);
            }

        }
    });

}

【问题讨论】:

  • 您希望它作为一项服务在应用程序关闭时继续运行,还是只在应用程序打开时运行?我认为您可能会在 Android 中遇到 Activity Lifecycle 问题。
  • 我的活动有两个按钮,发送和停止。当用户点击发送时,它会在每个间隔连续发送当前位置
  • 再一次,您希望在 Activity 在后台运行还是仅在 Activity 在屏幕上可见时运行?我认为您遇到了 Activity 生命周期的问题。
  • 活动在屏幕上时
  • 你是在模拟器上运行这个吗?

标签: android google-maps firebase firebase-realtime-database


【解决方案1】:

我看到有两件事情正在发生:

1) 仅记录最后记录的位置。你在哪里:

Firebase busCoords = mRef.child("Location");
busCoords.setValue(location.getLatitude()+ ",   "+location.getLongitude());

你应该输入:

Firebase busCoords = mRef.child("Location");
busCoords.push().setValue(location.getLatitude()+ ",   "+location.getLongitude());

这会将每个新位置作为列表项推送到您的 Firebase 数据库。

2) 坐标之间的距离也可能过大:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,1000,listener);

这预计在记录另一个位置之前至少有 1000 米的距离。您现在可以考虑在测试时将其更改为 2 或 3 米的间隔。

这有帮助吗?

【讨论】:

    【解决方案2】:

    使用 firebase 提供的 GeoFire Api 获取位置更新。在您的 onLocationChanged() 方法中,您必须这样做。

     @Override
    public void onLocationChanged(Location location) {
        geoFire.setLocation("location", new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
            @Override
            public void onComplete(String key, DatabaseError error) {
                if (error != null) {
                    Toast.makeText(MapsActivity.this, "There was an error saving the location to GeoFire: " + error, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MapsActivity.this, "Location saved on server successfully!", Toast.LENGTH_LONG).show();
    
                }
            }
        });
    }
    

    此方法每次更改时都会为​​您提供 LatLong。

    【讨论】:

    • 感谢您的帮助,我可以知道我应该将代码放在哪里发送到我的 firebase,
    • 已经写过 onLocation 改变了方法,但是在你的项目中添加 geofire api 之前
    • 我在哪里插入这个? Firebase busCoords = mRef.child("Location"); busCoords.setValue(location.getLatitude()+ ", "+location.getLongitude());
    猜你喜欢
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    相关资源
    最近更新 更多