Google Maps Marker群集?

时间:2018-04-10 14:02:20

标签: android google-maps

我正在尝试添加从firebase数据库中获取的标记集群。但是当我启动地图时,我的应用程序崩溃了错误:

  

java.lang.NullPointerException:尝试从空对象引用上的“double com.google.android.gms.maps.model.LatLng.longitude”字段中读取

这是我的MapActivity.java

public class MapActivity extends AppCompatActivity
        implements OnMapReadyCallback {

    private static final int DEFAULT_ZOOM = 15;
    private static final String TAG = MapActivity.class.getSimpleName();
    private LocationManager locationManager;
    private GoogleMap mMap;
    private ClusterManager<Marker> mClusterManager;
    private static final String EXTRA_MARKER_LIST = "EXTRA_MARKER_LIST";

    public static Intent getStartIntent(Context context, List<Marker> markerList) {
        Intent startIntent = new Intent(context, MapActivity.class);
        startIntent.putParcelableArrayListExtra(
               EXTRA_MARKER_LIST, new ArrayList<Marker>(markerList));
        return startIntent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_raw);

        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        mMap = googleMap;
        LatLng lviv = new LatLng(49.838, 24.029);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lviv, 12));
        mClusterManager = new ClusterManager<Marker>(this, googleMap);

        googleMap.setOnCameraIdleListener(mClusterManager);
        googleMap.setOnMarkerClickListener(mClusterManager);
        addMarkers();
    }

    private void addMarkers() {
        List<Marker> markerList=getIntent().getParcelableArrayListExtra(EXTRA_MARKER_LIST);
        for(int i = 0; i < markerList.size(); i++) {
            Marker marker = markerList.get(i);
            Location location = marker.getLocation();
            mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(location.getLatitude(), location.getLongitude()))
                    .title(marker.getName()));
            mClusterManager.addItem( marker);
        }
    }
}

这是我的模型类Marker.java

public class Marker implements Parcelable, ClusterItem {

    public String id;
    public String name;
    public String description;
    public String date;
    public Image image;
    public Location location;

    public Marker() {
    }

    protected Marker(Parcel in) {
        id = in.readString();
        name = in.readString();
        description = in.readString();
        date = in.readString();
        image = in.readParcelable(Image.class.getClassLoader());
        location = in.readParcelable(Location.class.getClassLoader());

    }
    public static final Creator<Marker> CREATOR = new Creator<Marker>() {
        @Override
        public Marker createFromParcel(Parcel in) {return new Marker(in);}
        @Override
        public Marker[] newArray(int size) {return new Marker[size];}
    };

    public String getDescription() {return description;}

    public String getId() {return id;}

    public String getDate(){return date;}

    public Image getImage() {return image;}

    public Location getLocation() {return location;}

    public String getName() {return name;}

    @Override
    public String toString() {
        return "Marker{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", date='" + date + '\'' +
                ", image=" + image +
                ", location=" + location + '}';
    }

    @Override
    public int describeContents() {return 0;}

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(id);
        parcel.writeString(name);
        parcel.writeString(description);
        parcel.writeString(date);
        parcel.writeParcelable(image, i);
        parcel.writeParcelable(location, i);
    }

    @Override
    public LatLng getPosition() {return null;}

    @Override
    public String getTitle() {return null;}

    @Override
    public String getSnippet() {return null;}
}

非常感谢你的关注。希望有人能帮助我

1 个答案:

答案 0 :(得分:0)

您必须在getPosition类中正确实现Marker,这是ClusterItem接口的实现。目前,您的Marker类为getPosition返回null。

所以改变这个:

@Override
public LatLng getPosition() {return null;}

类似于:

@Override
public LatLng getPosition() {
    return new LatLng(location.getLatitude(),location.getLongitude());
}

为此,请考虑实施其他接口方法getTitlegetSnippet

这些接口方法是ClusterManager从自定义类型参数中检索信息的方式。