[英]How to retrieve data from Firebase to make multiple Markers on map Android
this is my firebase look like 这是我的基地
And this my maps activity : 这是我的地图活动:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap mMap;
private DatabaseReference mUsers;
Marker marker;
public static final String FIREBASE_URL = "https://xxx.firebaseio.com/";
private Firebase firebase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
Firebase.setAndroidContext(this);
firebase = new Firebase(FIREBASE_URL);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
firebase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {
for (com.firebase.client.DataSnapshot s : dataSnapshot.child("marker").getChildren()) {
String lat = s.child("lat").getValue().toString();
String lng = s.child("lng").getValue().toString();
double latitude = Double.parseDouble(lat);
double longitude = Double.parseDouble(lng);
LatLng loc = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(loc).title(s.child("nama").getValue().toString()));
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}
and the error says 错误说
E/MarketPageConfiguration: parse page configuration json error
org.json.JSONException: No value for homeIndex
and when i run it, it doesn't show the marker and the location still get wrong. 当我运行它时,它不显示标记,并且位置仍然出错。 I think i name it wrong about the "child" and get value in it, but i have no idea how to figure it out. 我认为我对“孩子”的说法不对,并从中获得了价值,但我不知道如何弄清楚。
(the url link is all right) (网址链接可以)
To solve this, please change the following line of code: 要解决此问题,请更改以下代码行:
firebase = new Firebase(FIREBASE_URL);
to 至
firebase = new Firebase(FIREBASE_URL).child("marker");
You are missing a child from the tree, which is marker
and which is mandatory be used in your reference as well. 您从树中丢失了一个孩子,该孩子是marker
,也必须在参考中使用。
Further more, remove the .child("marker")
from the followig line of code: 此外,从以下代码行中删除.child("marker")
:
for (com.firebase.client.DataSnapshot s : dataSnapshot.child("marker").getChildren())
Shoul only be: 应该仅是:
for (com.firebase.client.DataSnapshot s : dataSnapshot.getChildren())
Also note, that you are using a very old version of Firebase and I recommend you update to the latest one. 另请注意,您使用的是Firebase的旧版本,建议您更新到最新版本。
I have tried with this one 我已经尝试过了
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
// public static final String FIREBASE_URL = "https://trans-k-1546744104547.firebaseio.com/";
private FirebaseDatabase firebase;
private DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
firebase = FirebaseDatabase.getInstance();
databaseReference = firebase.getReference("marker");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
String lat = child.child("lat").getValue().toString();
String lng = child.child("lng").getValue().toString();
double latitude = Double.parseDouble(lat);
double longitude = Double.parseDouble(lng);
LatLng loc = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(loc).title(child.child("nama").getValue().toString()));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.