android - 我想将 Latlng 值放入 Firebase 的 Google 地图中,但出现错误
问题描述
我希望解决这个问题,请帮助我解决这个问题。我正在使用 Resburrpi-3 将经度和纬度的位置数据上传到 firebase,现在我想在 latlong 参考上的谷歌地图上使用 firebase 检索经度和纬度。
private DatabaseReference mReference;
@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.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = mReference.child("Tezgam");
usersRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for (DataSnapshot snapm: dataSnapshot.getChildren()) {
latitude = snapm.child("lat").getValue(Long.class);
longitude = snapm.child("long").getValue(Long.class);
Speed = snapm.child("speed").getValue(String.class);
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Tezgam\n Current location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}`
解决方案
您的onMapReady()
方法可能在方法中的数据可用之前被触发onDataChanged()
。我假设您得到的错误类似于“纬度”和“经度”为空(假设,因为操作没有显示变量的声明方式)。这是一种竞争条件——一个方法在另一个方法提供数据之前完成。您必须适应丢失的数据。
所以改变你的onMapReady()
代码是这样的:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
updateMapLocation();
}
并将您的快照代码更改为:
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for (DataSnapshot snapm: dataSnapshot.getChildren()) {
latitude = snapm.child("lat").getValue(Long.class);
longitude = snapm.child("long").getValue(Long.class);
Speed = snapm.child("speed").getValue(String.class);
updateMapLocation();
}
}
现在,添加updateMapLocation()
将检查纬度和经度值是否为空的方法,否则显示位置:
private void updateMapLocation(){
if(latitude == null || longitude == null){
Log.e("updateMapLocation", "latitude or longitude is null")
}
else{
LatLng sydney = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Tezgam\n Current location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
推荐阅读
- python - 是否有一些代码的行为方式与 oracle 中的“add_month”相同?
- shell - 提取第三列中字符匹配的行
- elasticsearch - 将 Wkt(多面体)添加到 ElasticSearch 时解析异常
- sql - Identity_insert 设置为 OFF 错误,即使它设置为 ON
- c++ - 控制台中的 Unicode 表示问题
- java - 从 Hibernate 调用时,Mysql 存储过程返回结果集大小为零
- php - 如何在pdf中生成照片
- azure - 已删除 QNA 的搜索服务索引,如何重新创建?
- python - 基于不同数据帧中的日期时间的子集熊猫数据帧
- node.js - 始终在 Node.js 中获取 http 状态代码 200,即使服务器出现错误也是如此