【问题标题】:how to add google map marker from firebase database如何从firebase数据库添加谷歌地图标记
【发布时间】:2017-04-04 20:06:26
【问题描述】:

我想从 firebase 数据库中检索位置标记,但在运行此代码后我看不到任何标记。我试图制作EventListener,但地图运行时没有任何标记。这是我的代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

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

    }


    /**
     * 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;
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference();




                    String rightLocation = ref.child("location_right").toString();
                    String leftLocation = ref.child("location_left").toString();

                    double location_left = Double.valueOf(leftLocation);
                    double location_right = Double.valueOf(rightLocation);
                    LatLng sydney = new LatLng(location_left, location_right);
                    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,18));











        // Add a marker in Sydney and move the camera

    }
}

这里可能有什么问题?

【问题讨论】:

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


    【解决方案1】:

    您需要使用ValueEventListener 从 Firebase 数据库获取数据。这在the documentation 中有解释。

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    double location_left = dataSnapshot.child("location_left").getValue(Double.class);
                    double location_right = dataSnapshot.child("location_right").getValue(Double.class);
                    LatLng sydney = new LatLng(location_left, location_right);
                    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,18));
                } else {
                    Log.e(TAG, "onDataChange: No data");
                }
    
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException();
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      相关资源
      最近更新 更多