首页 > 解决方案 > 我想将 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));
}`

标签: androidgoogle-mapsfirebase-realtime-database

解决方案


您的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));
    }
}

推荐阅读