Skip to content Skip to sidebar Skip to footer

Multiple Markers On Map With Different Icons And Titles

I have updated my question please review it Iam developing Blood Donar app in android.In that Iam using Google Map to display the donar on map.To show multiple markers on map I am

Solution 1:

First of all, you should not mMap.animateCamera and mMap.moveCamera inside the loop. After loop, you should set your camera location and zoom to any one place.

for (int i = 0; i < arrayList.size(); i++) {
    LatLng latLngForMarker = new LatLng(Double.parseDouble(arrayList.get(i).getLat()), Double.parseDouble(arrayList.get(i).getLng()));

    MarkerOptions markerOptions = new MarkerOptions().position(latLngForMarker);
    markerOptions.title(arrayList.get(i).getUserName());
    markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon));
    mMap.addMarker(markerOptions.position(latLngForMarker).title(arrayList.get(i).getUserName()));
}

// Then you can set default zoom to any one location:// default latitude and longitude for init and zoom camera, you can change by your requirementdouble defaultLatitude = 23.0201818;
double defaultLongitude = 72.4396566;
CameraUpdate centerGujarat = CameraUpdateFactory.newLatLng(new LatLng(defaultLatitude, defaultLongitude));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);
mMap.moveCamera(centerGujarat);
mMap.animateCamera(zoom);

Solution 2:

As per above code snippet, both are async process and you are trying to call it both together.

when you try to add marker you should check whether isMapReady or not also as per above crash latlng value are null, you should wait until data load from firebase method load callback.

So ideally it would be

privatevoidfun() {
        databaseReference = FirebaseDatabase.getInstance().getReference().child(Constants.content).child(Constants.profiles).child("lRSFuI5pvBZh1VZQWHI4ev52gXF2");
        databaseReference.addListenerForSingleValueEvent(newValueEventListener() {
            @OverridepublicvoidonDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (DataSnapshot parent : dataSnapshot.getChildren()) {
                        Profiles profiles = parent.getValue(Profiles.class);
                        name = profiles.getName();
                        String email = profiles.getEmail();
                        lat = Double.valueOf(profiles.getLatitude());
                    lon = Double.valueOf(profiles.getLongitude());
                        type = profiles.getType();

                //Add your marker code here with map ready check. 

                    }
    }

Solution 3:

After alot of effort I have find a way to solve this issue I still didn't understand how its worked because its was an issue of calling function properly and according to me I was doing fine but still with some minor amendments I got it Firstly I have created a Array list of MarkerOptions and added marker data in it where Iam getting data from my firebase Database and in the same function I then added a For Loop in which I start adding my markers and then call this function in OnMapReady Function Like This :

privatevoidgettingMarkersData() {

        databaseReference = FirebaseDatabase.getInstance().getReference().child(Constants.content).child(Constants.profiles);
        databaseReference.addListenerForSingleValueEvent(newValueEventListener() {
            @OverridepublicvoidonDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (DataSnapshot parent : dataSnapshot.getChildren()) {

                        Profiles profiles = parent.getValue(Profiles.class);
                        String name = profiles.getName();
                          latD = Double.valueOf(profiles.getLatitude());
                        lonD = Double.valueOf(profiles.getLongitude());
                        int type = profiles.getType();

                        if (type == 1) {
                            MarkerOptions markerOptions = newMarkerOptions().position(newLatLng(latD, lonD)).title(name);
                            marker.add(markerOptions);
                        } elseif(type == 2) {
                            MarkerOptions markerOptions = newMarkerOptions().position(newLatLng(latD, lonD)).title(name)
                                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_bloodbank));
                            marker.add(markerOptions);
                        }

                    }
                    for (int i = 0; i < marker.size(); i++) {

                        mMap.addMarker(marker.get(i));
                    }

                }

            }

            @OverridepublicvoidonCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

And then in OnMapReady I call this function :

publicvoidonMapReady(GoogleMap googleMap) {

        mMap = googleMap;
        mMap.setOnCameraIdleListener(onCameraIdleListener);

        gettingMarkersData();
/* for (int i = 0; i < marker.size(); i++) {
                    mMap.addMarker(marker.get(i));
                }*/

        mMap.animateCamera(CameraUpdateFactory.zoomTo(14.0F));
        mMap.getUiSettings().setZoomControlsEnabled(true);

    }

Before doing this I was adding markers in OnMapReady (the for loop which I have commented ) for adding the markers and it was not adding markers then I paste it in gettingMarkersData(). and its start working fine.

Post a Comment for "Multiple Markers On Map With Different Icons And Titles"