读取 longlat 数据实时 Firebase 和实现以映射 Android Studio

Posted

技术标签:

【中文标题】读取 longlat 数据实时 Firebase 和实现以映射 Android Studio【英文标题】:Read longlat data realtime Firebase and implementation to map Android Studio 【发布时间】:2019-01-08 15:41:27 【问题描述】:

你能帮我解决这个问题吗?

我想从 Firebase 实时读取数据并在地图上显示标记。 longlat 标记数据是从 firebase 而非代码中实时读取的。 Firebase 更改了地图中的数据。

这是我的代码:

public class mapmotor1 extends FragmentActivity implements OnMapReadyCallback 

private GoogleMap mMap;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReference;
private static final int LOCATION_REQUEST = 500;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mapmotor1);

    // 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);

    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference().child("proyek-akhir-c93d7").child("1");



/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) 
    mMap = googleMap;
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference().child("proyek-akhir-c93d7").child("1");
   **//this is replaced what**
    LatLng device = new LatLng (-6.97455, 107.6326);

    mMap.addMarker(new MarkerOptions().position(device).title("Here"));

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(device, 15));
    mMap.animateCamera(CameraUpdateFactory.zoomIn());
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);


@SuppressLint("MissingPermission")
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) 
    switch (requestCode)
        case LOCATION_REQUEST:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                mMap.setMyLocationEnabled(true);
            
            break;
    

【问题讨论】:

【参考方案1】:

假设proyek-akhir-c93d7是你的Firebase根,要获取1节点下的经纬度,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference oneRef = rootRef.child("1");
ValueEventListener valueEventListener = new ValueEventListener() 
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) 
        double latitude = dataSnapshot.child("latitude").getValue(Double.class);
        double longitude = dataSnapshot.child("longitude").getValue(Double.class);
        //Do what you need to do with the latitude and longitude
        Log.d("TAG", latitude + ", " + longitude);
    

    @Override
    public void onCancelled(DatabaseError databaseError) 
;
oneRef.addListenerForSingleValueEvent(valueEventListener);

您的 logcat 中的输出将是:

-6.97455, 107.6326

【讨论】:

"oneRef.addListenerForSingleValueEvent(valueEventListener);"代码放在哪里?在 public void onCancelled 或 dataonchange 之下? 不,就在你在我的代码中看到的位置,就在最后一个花括号和分号之后,对吧?有用吗?

以上是关于读取 longlat 数据实时 Firebase 和实现以映射 Android Studio的主要内容,如果未能解决你的问题,请参考以下文章

Android-从Firebase实时数据库读取数据并显示在RecyclerView上

无法读取或写入 firebase 实时数据库(android studio)

在 Flutter 中从 Firebase 实时数据库读取和存储数据

从 Google Firebase 实时数据库读取 google maps 标记数据时出现问题

Firebase 实时数据库中有关身份验证的信息

在 SwiftUI 中读取带有子节点的 Firebase 实时数据库父节点的问题