Multiple Markers on Map with different icons and titles

这一生的挚爱 提交于 2020-01-02 18:36:08

问题


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 trying this code but its crashing the application

public class Profiles {
String name;
String email;
String latitude;
String longitude;
int type;

public Profiles() { }

public Profiles(String name, String email, String latitude, String longitude , int type) {
    this.name = name;
    this.email = email;
    this.latitude = latitude;
    this.longitude = longitude;
    this.type = type;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public String getLongitude() {
    return longitude;
}

public void setLongitude(String longitude) {
    this.longitude = longitude;
}

public int getType() {
    return type;
}

public void setType(int type) {
    this.type = type;
}

}

and in MapsActivity Iam getting it like

 private void fun() {
            databaseReference = FirebaseDatabase.getInstance().getReference().child(Constants.content).child(Constants.profiles).child("lRSFuI5pvBZh1VZQWHI4ev52gXF2");
            databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@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();

                        }
        }

     @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;

            if (type == 1) {
                marker = new MarkerOptions().position(new LatLng(lat, lon)).title(name);
            } else {
                marker = new MarkerOptions().position(new LatLng(lat, lon)).title(name)
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_bloodbank));
            }
            mMap.setOnCameraIdleListener(onCameraIdleListener);
            mMap.addMarker(marker);
        }

I get this error for this

If I get this all detail in any other activity it gives me everything wihtout crashing but when I try to use it in Maps Activity it crashes the app direct giving this error.I can understand why its crashing while Iam getting detail in other activities I have tried for testing and getting perfectly all in toast and logcat.


回答1:


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 :

private void gettingMarkersData() {

        databaseReference = FirebaseDatabase.getInstance().getReference().child(Constants.content).child(Constants.profiles);
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@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 = new MarkerOptions().position(new LatLng(latD, lonD)).title(name);
                            marker.add(markerOptions);
                        } else if(type == 2) {
                            MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(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));
                    }

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

And then in OnMapReady I call this function :

public void onMapReady(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.




回答2:


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 requirement
double 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);



回答3:


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

private void fun() {
        databaseReference = FirebaseDatabase.getInstance().getReference().child(Constants.content).child(Constants.profiles).child("lRSFuI5pvBZh1VZQWHI4ev52gXF2");
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@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. 

                    }
    }


来源:https://stackoverflow.com/questions/59370031/multiple-markers-on-map-with-different-icons-and-titles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!