如何从firebase数据库检索经度和经度到android地图应用程序

大道之行,天下为公。这篇文章主要讲述如何从firebase数据库检索经度和经度到android地图应用程序相关的知识,希望能为你提供帮助。
我正在开发一个包含驱动程序应用程序和学生应用程序的总线跟踪应用程序。所以我可以将驱动程序应用程序的坐标存储到firebase实时数据库中。但是当我试图检索坐标并试图用标记在地图上显示时,应用程序立即崩溃。我不知道为什么会这样,我现在正在关注YouTube视频课程。
非常感谢帮助。
【如何从firebase数据库检索经度和经度到android地图应用程序】所以这是一个firebase数据库,所以我只是为了测试目的而给予了读写权限,应该就像当我按下按钮它应该找到最近的公交车骑手,从firebase和store检索坐标在一个双变量中,并在地图上显示它们。
所以对于驱动程序我使用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“Driver Key Found”但在此之后应用程序崩溃而地图上没有任何标记。你们也可以观看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数据库输出,请告诉我,我将发布带有数据的图像。
答案您可以使用以下数据库引用类方法从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)) } })


    推荐阅读