好吧我的问题很简单,我希望我的谷歌地图在地图上显示所选位置,我将自己的纬度和经度插入数据库而不是通过编程代码,不确定这是否会影响任何东西,但我想我让你们都知道。我在我的代码中使用了两种方法,每种方法都给了我不同的错误。不确定我使用了正确的方法,但我需要你的帮助,几乎完成我的应用程序。
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();
}
我的数据库:
使用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)
在这里,我们可以看到它获得了我想要的正确数据。
如果信息不足,请告诉我。提前谢谢。
答案 0 :(得分:1)
这些变化应该会产生更好的结果:
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();
}
}