如何从firebase数据库添加谷歌地图标记 [英] how to add google map marker from firebase database

查看:126
本文介绍了如何从firebase数据库添加谷歌地图标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$ p $ public class MapsActivity扩展FragmentActivity实现OnMapReadyCallback {

私有GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//获取SupportMapFragment并在地图准备好使用时得到通知。
SupportMapFragment mapFragment =(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

}


/ **
*操作一次可用的地图。
*当地图准备好被使用时,这个回调被触发。
*这是我们可以添加标记或线条,添加听众或移动相机的地方。在这种情况下,
*我们只在澳大利亚悉尼附近添加一个标记。
*如果设备上没有安装Google Play服务,系统会提示用户在SupportMapFragment中安装
* it。只有当用户安装了
* Google Play服务并返回到应用后,此方法才会被触发。
* /
@Override
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;
DatabaseReference ref = FirebaseDatabase.getInstance()。getReference();




字符串rightLocation = ref.child(location_right)。toString();
字符串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));











// b在悉尼添加一个标记并移动相机


$ b code





$ b <

解决方案

您需要使用 ValueEventListener 从Firebase数据库获取数据。这在文档中进行了解释。

  @Override 
public void onMapReady(GoogleMap googleMap){
mMap = googleMap;
DatabaseReference ref = FirebaseDatabase.getInstance()。getReference();

ref.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){$ b $ 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();
}
});


I want to retrieve location markers from firebase database but I can't see any markers after running this code. I tried to make EventListener but the map runs without any markers. This my code:

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

    }
}

What might be wrong here?

解决方案

You need to use a ValueEventListener to obtain data from the Firebase database. This is explained in 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();
        }
    });

这篇关于如何从firebase数据库添加谷歌地图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆