如何检索数据库火力点的纬度和经度到Android地图应用

时间:2019-02-02 21:08:53

标签: java android firebase location geofire

我正在开发一个公交车跟踪应用程序,其中包含Driver应用程序和Student应用程序。所以,我可以从驱动器应用的坐标存储火力实时数据库。但是,当我尝试检索坐标并尝试在带有标记的地图上显示坐标时,应用程序立即崩溃。 我不知道为什么会这样,目前我正在关注youtube视频课程。

我们将不胜感激。

所以这是一个Firebase数据库,所以我已经出于测试目的授予了读写权限,就像当我按下按钮时,它应该找到最近的公交车驾驶员,从中检索坐标火力并存储在双变量,并将它们显示在地图上。

所以对于驱动程序,我正在使用Geofire将位置存储在firebase中,这是代码:

   @Override
   public void onLocationChanged(Location location) {
      if(getApplicationContext() != null){

        mLastLocation = location;

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new 
LatLng(location.getLatitude(), location.getLongitude()), 16.5f));

        String UserId =      
Objects.requireNonNull(FirebaseAuth.getInstance()
.getCurrentUser()).getUid();
        DatabaseReference refAvail = FirebaseDatabase.getInstance().getReference("Location").child("Driver");

        GeoFire geoFire = new GeoFire(refAvail);
        geoFire.setLocation(UserId, new GeoLocation(location.getLatitude(), location.getLongitude()));
    }
}

对于客户端应用程序,我已经尝试过:

mCFS.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pickuplocation = new LatLng(mLastLocation.getLatitude(), 
mLastLocation.getLongitude());

            mMap.addMarker(new 
MarkerOptions().position(pickuplocation).title("Student is here"));

            mCFS.setText("Sending Location");

            getClosestDriver();

        }
    });
}

带有一些变量的getClosestDriver()和getDriverLocation()方法:

private int radius = 1;

private Boolean driverFound = false;
private String driverFoundID;

private void getClosestDriver(){
    DatabaseReference DriverLocation = 
FirebaseDatabase.getInstance().getReference("Location").child("Driver");

    GeoFire geoFire = new GeoFire(DriverLocation);

    GeoQuery geoQuery = geoFire.queryAtLocation(new 
GeoLocation(pickuplocation.latitude, pickuplocation.longitude), radius);
    geoQuery.removeAllListeners();

    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            if(!driverFound)
            {
                driverFound = true;
                driverFoundID = key;
                Toast.makeText(getApplicationContext(), "Driver KEY 
Found", Toast.LENGTH_SHORT).show();

                getDriverLocation();
                mCFS.setText("Looking For Driver Location");
            }
        }

        @Override
        public void onKeyExited(String key) {

        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {

        }

        @Override
        public void onGeoQueryReady() {
            if(!driverFound){
                radius++;
                getClosestDriver();
            }
        }

        @Override
        public void onGeoQueryError(DatabaseError error) {

        }
    });
}


Marker mMarker;
private void getDriverLocation(){
    DatabaseReference DLref = 
FirebaseDatabase.getInstance().getReference("Location")
.child("Driver").child(driverFoundID).child("l");
    DLref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                List<Objects> map = (List<Objects>) 
dataSnapshot.getValue();
                double locationLat = 0;
                double locationLng = 0;
                mCFS.setText("Driver Found");

                assert map != null;
                if(map.get(0) != null){
                    locationLat = 
Double.parseDouble(map.get(0).toString());
                }

                if(map.get(1) != null){
                    locationLng = 
Double.parseDouble(map.get(1).toString());
                }
                LatLng DriverLatLng = new LatLng(locationLat, 
locationLng);
                if(mMarker != null){
                    mMarker.remove();
                }
                mMarker = mMap.addMarker(new 
MarkerOptions().position(DriverLatLng).title("Bus"));
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

} 

在我的输出中,我得到了Toast“找到驱动程序密钥”,但是之后应用程序崩溃了,地图上没有任何标记。 你们还可以观看Simcoder尤伯杯克隆教程#7,8,9我按照一个让我的愿望输出。

应用崩溃后,这是Logcat输出:

02-03 10:28:13.023 32241-32241/com.example.clientapplication 
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.clientapplication, PID: 32241
java.lang.ClassCastException: java.lang.Double cannot be cast to 
java.util.Objects
    at 
com.example.clientapplication.ClientMap$4.onDataChange
(ClientMap.java:163)
    at 

com.google.firebase.database.core.ValueEventRegistration.fireEvent
(com.google.firebase:firebase-database@@16.0.6:75)
    at 
 com.google.firebase.database.core.view.DataEvent.fire
(com.google.firebase:firebase-database@@16.0.6:63)
    at 
 com.google.firebase.database.core.view.EventRaiser$1.run
(com.google.firebase:firebase-database@@16.0.6:55)
    at android.os.Handler.handleCallback(Handler.java:742)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5601)
    at java.lang.reflect.Method.invoke(Native Method)
    at 
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:774)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)

如果你们想要我的Firebase数据库输出,请告诉我,我将发布其中包含数据的图像。

1 个答案:

答案 0 :(得分:1)

您可以使用以下数据库引用类的方法从firebase检索数据。

//Get the reference of parent node...
firebaseDatabase= FirebaseDatabase.getInstance()
databaseReference=firebaseDatabase?.getReference("student")

//Create the listener to fetch data...
databaseReference?.addValueEventListener(object :ValueEventListener{
override fun onCancelled(p0: DatabaseError)
{ }

override fun onDataChange(p0: DataSnapshot)
{
lststudent.clear()
var child=p0.children
child.forEach {
     var map=it.value as HashMap<String, String>
     lststudent.add(map)
}
//here you have to use the data fetch from real time databse in outside this method it will reset the data...
lvstud.adapter= SimpleAdapter(this@MainActivity,
                        lststudent,
                        R.layout.student,
                        arrayOf("studname","studmobno"),
                        intArrayOf(R.id.tvStudname,R.id.tvStudmobno))
            }
})