首页 > 解决方案 > 谷歌地图标记标题显示是完整列表

问题描述

我现在正在使用 google maps 项目并从 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()) {

                     // System.out.println(chidSnap.getKey()); //displays the key for the node
                    // System.out.println(chidSnap.child("Latitude").getValue());
                    // System.out.println( chidSnap.child("Longitude").getValue());   //gives the value for given keyname


                    //some latitude and logitude value

                    //  System.out.println(latlngs);   //gives the value for given keyname

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


                if(mMap != null) {




                    for (LatLng point : latlngs) {
                        options.position(point);
                        options.title(String.valueOf(Names));
                        options.snippet("someDesc");
                        mMap.addMarker(options);
                    }
                }



            }
            @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(String.valueOf(Names));
            options.snippet("someDesc");
            googleMap.addMarker(options);
        }


    }
}

我的 MapsActivity 上面。我期待的是一个标记应该命名为Lex B05,另一个用户应该是amanda提前感谢您的帮助!..

标签: javaandroid

解决方案


那是因为您将相同的 arrayList 名称传递给所有标记,请在添加标记时尝试以下代码

for (int i=0;i< latlngs.size();i++) {
            LatLng point=latlngs.get(i);
            options.position(point);
            options.title(Names.get(i));
            options.snippet("someDesc");
            mMap.addMarker(options);
        }

希望这会有所帮助!


推荐阅读