Firebase数据库无法检索纬度和经度并在地图上显示

qodio 发布于 2019-10-09 android 最后更新 2019-10-09 22:59 11 浏览

好吧,我的问题很简单,我希望我的谷歌地图在地图上显示选定的位置,我自己将纬度和经度插入到数据库中,而不是通过编程代码,不确定这是否会影响任何内容,但我想我让你们知道。我使用了两种方法,我在代码中注释了它们,每个方法都给了我不同的错误。不知道我使用了正确的方法,但我需要你们的帮助,几乎完成了我的应用程序。 MapsActivity:

private void getLocation(){
    databaseLocation.child("Location").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
Bundle b = getIntent().getExtras();
            if (b != null) {
                String sports = b.getString("TeamSport");
for (DataSnapshot child : dataSnapshot.getChildren()) {
                    if (child.getKey().equals(sports)) {
                        if (child.getKey().equals("latitude")) {
                            //latitude = Double.parseDouble(String.valueOf(child.getValue()));
                            //latitude = child.getValue(Double.class);
                        } else {
                            //longitude = Double.parseDouble(String.valueOf(child.getValue()));
                            //longitude = child.getValue(Double.class);
                        }
// Create LatLng for each locations
                        LatLng latlng = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(latlng).draggable(true));
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
                        //Setting onMarkerDragListener to track the marker drag
                        mMap.setOnMarkerDragListener(MapsActivity.this);
                        //Adding a long click listener to the map
                        mMap.setOnMapLongClickListener(MapsActivity.this);
moveMap();
                    } else {
                        Toast.makeText(getApplicationContext(), "No location found.", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
}
    });
}
@Override
public void onConnected(Bundle bundle) {
getLocation();
}
我的数据库: enter image description here 使用child.getvalue()时出错:
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.util.HashMap to double
                                                                        at com.google.android.gms.internal.zzbtg.zzaD(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbtg.zzc(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                                        at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                        at com.example.user.stfv2.MapsActivity$1.onDataChange(MapsActivity.java:148)
                                                                        at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
                                                                        at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
使用parseDouble方法时出错:
java.lang.NumberFormatException: Invalid double: "{latitude=1.857204, longitude=103.087338}"
                                                                      at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                                      at java.lang.StringToReal.initialParse(StringToReal.java:114)
                                                                      at java.lang.StringToReal.parseDouble(StringToReal.java:282)
                                                                      at java.lang.Double.parseDouble(Double.java:301)
                                                                      at com.example.user.stfv2.MapsActivity$1.onDataChange(MapsActivity.java:147)
                                                                      at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                                                                      at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                                                                      at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
                                                                      at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
在这里,我们可以看到它获得了我想要的正确数据。 如果信息不足,请告诉我。提前致谢。
已邀请:

nrerum

赞同来自:

这些变化应该产生更好的结果:

for (DataSnapshot child : dataSnapshot.getChildren()) {
    if (child.getKey().equals(sports)) {
        // child is a snapshot of a sport location; Basketball or Football
        // latitude and longitude are children of the sport shapshot
        latitude = child.child("latitude").getValue(Double.class);
        longitude = child.child("longitude").getValue(Double.class);
// Create LatLng for each locations
        LatLng latlng = new LatLng(latitude, longitude);
...
    } else {
        Toast.makeText(getApplicationContext(), "No location found.", Toast.LENGTH_LONG).show();
    }
}