当我启动此活动时,我的应用程序停止。我需要从数据库获取坐标并放置标记。我不知道问题在哪里。
我的数据库:
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css" rel="stylesheet"/>
<body>
<div id="homeBody">
<div class="row">
<div class="col s12 m12 no-padding">
<div class="card">
<div class="card-content white-text center-align scm-primary scm-card-content">
Europe - Champions League
</div>
<div class="card-action no-padding">
<!-- Partidas -->
<div class="row no-padding">
<div class="col s12 m12">
<div class="card scm-match-card">
<div class="card-content scm-card-content">
<div class="row no-padding">
<div class="col s8 small-text scm-primary-text">Paris Saint Germain X Atlético de Madrid</div>
<div class="col s4 right small-text right-align scm-primary-text">30/12 - 15:00</div>
</div>
</div>
<div class="card-action center-align scm-card-content">
<div class="row">
<div class="col s3 m3 l4">
<a class="scm-odd-button">
<span>
<strong>2,00</strong>
<br>
<small>Home</small>
</span>
</a>
</div>
<div class="col s3 m3 l4">
<a class="scm-odd-button">
<span>
<strong>2,00</strong>
<br>
<small>Away</small>
</span>
</a>
</div>
<div class="col s3 m3 l4">
<a class="scm-odd-button">
<span>
<strong>2,00</strong>
<br>
<small>Paris Saint Germain/Atlético de Madrid</small>
</span>
</a>
</div>
<div class="col s3 m3 l4">
<a class="scm-odd-button">
<span>
<strong>+</strong>
<br>
<small>More</small>
</span>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
答案 0 :(得分:1)
尝试这样来检索lat和lng
NULL
答案 1 :(得分:0)
首先,如果您想要获取所有博客的孩子,那么您应该将您的参考文献放在DatabaseReference f_database = FirebaseDatabase.getInstance().getReference("Blog");
将firebase引用放在DatabaseReference f_database = FirebaseDatabase.getInstance().getReference()
上时。这将获取数据库的所有节点,例如博客和用户。如果您需要博客信息,则无需获取用户数据。对于 firebase ,这是一个非常糟糕的代码实践。
其次,正如@Benjith binja建议您可以获取所有博客的孩子来获取纬度&amp;经度。或者,您应该创建一个Blog对象并获取数据。这是一个很好的做法。 https://firebase.google.com/docs/database/android/read-and-write
博客对象:
class Blog {
public String desc,id,image,time,title,uid,username;
public Double lat,lng;
public Blog(String desc, String id, String image, String time, String title, String uid, String username, Double lat, Double lng) {
this.desc = desc;
this.id = id;
this.image = image;
this.time = time;
this.title = title;
this.uid = uid;
this.username = username;
this.lat = lat;
this.lng = lng;
}
}
firebase监听器看起来像这样:
DatabaseReference f_database = FirebaseDatabase.getInstance().getReference("Blog");
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
// Get Blog object and use the values to update the UI
Blog blog = snapshot.getValue(Blog.class);
LatLng location = new LatLng(blog.lat,blog.lng);
googleMap.addMarker(new MarkerOptions().position(location).title("pedik"));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
f_database.addListenerForSingleValueEvent(listener);
由于Map的使用错误,必须发生错误。以下是代码的外观。
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
f_database.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
Map data = (Map) child.getValue();
Map mCoordinate = (HashMap)data.get("Blog");
//mCoordinate is a map with values of all the children of Blog
for(String key: mCoordinate.keySet()){
Map mCoordinateChildMap = (HashMap) mCoordinate.get(key)
double latitude = (double) (mCoordinateChildMap.get("lat"));
double longitude = (double) (mCoordinateChildMap.get("lng"));
LatLng location = new LatLng(latitude,longitude);
googleMap.addMarker(new MarkerOptions().position(location).title("pedik"));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});