【问题标题】:I want to put Latlng value In Google Maps from Firebase, but I am getting errors我想将 Latlng 值放入 Firebase 的 Google 地图中,但出现错误
【发布时间】:2019-02-16 02:14:34
【问题描述】:

我希望解决此问题,请帮助我解决此问题。 我正在使用 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));
}`

【问题讨论】:

  • 那么到底是什么问题呢?您是否收到任何错误消息并且可以共享堆栈跟踪?代码是否正在运行但未按预期工作?你一步步调试过吗?
  • 请添加您的数据库结构。

标签: android google-maps firebase-realtime-database


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-07-14
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 2021-01-06
    • 2020-10-21
    相关资源
    最近更新 更多