【问题标题】:How to retrieve data from Firebase database and show the values in Google map?如何从 Firebase 数据库中检索数据并在 Google 地图中显示值?
【发布时间】:2018-11-21 10:41:27
【问题描述】:

在我的数据库中,我有像database structure 这样的每个人的位置值,我想检索数据并在地图中显示。如何获取和显示?我创建了一个ArrayList 和详细信息类构造函数以获取值。

【问题讨论】:

  • 欢迎来到 StackOverflow。请edit您的问题并发布一些minimal reproducible example。因此,我们可以很容易地了解您到目前为止所做的尝试以及您面临的问题。

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


【解决方案1】:

使用以下代码:

    @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference locationsRef = rootRef.child("yourchildname");
    final GeoFire geoFire = new GeoFire(locationsRef);
        geoFire.getLocation("idofyourdata", new LocationCallback() {
            @Override
            public void onLocationResult(String key, GeoLocation location) {
                double lat =location.latitude;
                double lng =location.longitude;
                LatLng sydney = new LatLng(lng, lat);
                mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(HUE_BLUE)).position(sydney).title("car"));
                Toast.makeText(getApplicationContext(),""+sydney,Toast.LENGTH_SHORT).show();
                mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(9));
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mMap = googleMap;
    mMap.setMyLocationEnabled(true);
    buildGoogleApiClient();

}

【讨论】:

    【解决方案2】:

    要解决这个问题,请使用以下代码:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference locationsRef = rootRef.child("Locations");
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String latitude = ds.child("Latitude").getValue(String.class);
                String longitude = ds.child("Longitude").getValue(String.class);
                LatLng latLng = new LatLng(String.valueOf(latitude), String.valueOf(longitude));
                MarkerOptions markerOptions = new MarkerOptions().position(latLng).title("My Marker");
                Marker marker = googleMap.addMarker(markerOptions);
            }
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    locationsRef.addListenerForSingleValueEvent(valueEventListener);
    

    但我强烈建议您将 latitudelongitude 的值存储为 doubles 而不是字符串。

    【讨论】:

    • 感谢,当 Firebase 中的一个位置更新时,有两个或取决于更新时间,标记在地图上每次都显示相同的设备。 (我使用 ds.child("Email").getValue(String.class 作为标题),有时我有四个相同标题的标记。你能给出解决办法吗
    • 如果标记在移动,那么只需从较早的位置移除标记,对吗?
    • 请问可以提供示例代码吗?这将是一个很大的帮助 thnks
    • 最初的问题是How to retrieve data from Firebase database and show the values in Google map?,为此我刚刚给出了答案。如果您有其他问题,您应该根据评论中的信息自行尝试,或者如果出现其他问题,请提出其他问题。所以万一出现问题,为了遵守本社区的规则,请再次提出新问题,以便我和其他用户可以帮助您。
    • 好的,其实你的回答对我很有帮助,谢谢你的帮助
    猜你喜欢
    • 2020-05-09
    • 2017-12-15
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多