【问题标题】:Google maps plotting multiple markers from firebase database谷歌地图从firebase数据库中绘制多个标记
【发布时间】:2020-01-29 18:44:58
【问题描述】:

在地图上绘制我的标记时遇到麻烦,我能够从 firebase 获取我的用户的纬度和经度并将其存储到我的数组列表中,我的问题是我如何能够全局设置我的数组列表?这是我的代码。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private GoogleMap googleMap;
    private MarkerOptions options = new MarkerOptions();
    private ArrayList<LatLng> latlngs = new ArrayList<>();
    private ArrayList<String> Names = new ArrayList<>();
    String Lat ,Lon,Names1;
    double latitude , longitude;







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





        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("Positions");
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {

                    Lat = String.valueOf(chidSnap.child("Latitude").getValue());
                    Lon = String.valueOf(chidSnap.child("Longitude").getValue());
                    Names1 = String.valueOf(chidSnap.getKey());

                    latitude= Double.parseDouble(Lat);
                    longitude= Double.parseDouble(Lon);


                    latlngs.add(new LatLng(latitude, longitude));
                    Names.add(Names1);




                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });


       // System.out.println(Names);


    }









    /**
     * 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;
        for (LatLng point : latlngs) {
            options.position(point);
            options.title("Users");
            options.snippet("someDesc");
            googleMap.addMarker(options);
        }


    }
}

我不知道这东西出了什么问题,不知何故我的地图没有显示任何标记,我的列表“latlngs”显示“0.0,0.0” 我不知道如何在全局范围内设置我的 latlngs,因为它位于从 firebase 获取数据的代码中。

【问题讨论】:

    标签: java android


    【解决方案1】:

    对 Firebase 的查询是一个异步过程。因此,您的onDataChange 可能在onMapReady 之后执行。试试下面:

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    
        for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {
    
            ....
        }
    
        if(mMap != null) {
            for (LatLng point : latlngs) {
                options.position(point);
                options.title("Users");
                options.snippet("someDesc");
                mMap.addMarker(options);
            }
        }
    } 
    

    或者onDataChange内部调用mapFragment.getMapAsync(MapsActivity.this);

    【讨论】:

    • 是的! ,现在它的工作,非常感谢你!它对我帮助很大!
    • @Md. Asaduzzaman 一件事,我所有的标记都是 Titled to Users ,如何将标题设置为对应的用户名?
    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多