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



我正在开发一个总线跟踪应用程序,其中包含Driver应用程序和Student应用程序。因此,我可以将坐标从驱动程序应用程序存储到firebase实时数据库。但当我试图检索坐标并试图用标记在地图上显示它们时,应用程序立即崩溃。我不知道为什么会发生这种情况,我目前正在关注youtube上的视频课程。

我们将不胜感激。

所以这是一个firebase数据库,所以我只为了测试目的而允许读取和写入,就像当我按下按钮时,它应该找到最近的公交车乘客,从firebase检索坐标,并存储在一个双变量中,并将其显示在地图上一样。

所以对于司机来说,我使用Geofire将位置存储在防火区,代码如下:

@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))
}
})

最新更新