地图上具有不同图标和标题的多个标记

问题描述 投票:1回答:2

我已更新我的问题,请复查我在android中开发Blood Donar应用程序。在那次Iam中,我使用Google Map在地图上显示了Donar。要在地图上显示多个标记,我正在尝试使用此代码,但该应用程序崩溃了

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

}

和在MapsActivity Iam中获得的效果

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

我为此收到此错误

enter image description here

[如果我在任何其他活动中都获得了所有详细信息,它会给我带来崩溃的一切,但是当我尝试在Maps Activity中使用它时,它会使应用程序直接崩溃,并给出此错误。我可以理解为什么我在其他活动中获得详细信息时却崩溃了我已经尝试过测试,并完全掌握了吐司和logcat。

android google-maps google-maps-markers
2个回答
0
投票
您可以将数据类与latlng,图标,标题一起使用。要显示更多详细信息,您可以使用信息窗口。

data class Person( val latlng: LatLng? = null, val name: String? = null,//Name of person val icon: Bitmap? = null //It could be drawable id or bitmap(https://stackoverflow.com/a/20465313/2255073) ) for(int i = 0;i<arrayList.size();i++){ mMap.addMarker(new MarkerOptions().position(arrayList.get(i).latlng).title(arrayList.get(i).name)); mMap.animateCamera(CameraUpdateFactory.zoomTo(15.0F)); mMap.moveCamera(CameraUpdateFactory.newLatLng(arrayList.get(i))); }

有关信息窗口的更多信息,您可以参考https://developers.google.com/maps/documentation/android-sdk/infowindows

0
投票
首先,您不应在循环内使用mMap.animateCameramMap.moveCamera。循环后,您应该设置相机位置并缩放到任意一个位置。

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

© www.soinside.com 2019 - 2024. All rights reserved.