首页 > 解决方案 > 如何将 latlng 数组列表的值传递给多边形

问题描述

我将坐标存储在firebase中。并且始终作为 latlng 类型的数组列表检索。我的问题是如何将坐标数组的值传递给多边形多边形。

这是我尝试过的。

private void DrawPolygon(List<LatLng> array) {
    int length = array.size();
    // Store a data object with the polygon, 
    // used here to indicate an arbitrary type.
    PolygonOptions poly = new PolygonOptions();

    for (int i = 0; i < length; i++) {
        poly.add(new LatLng(array.get(i).latitude, array.get(i).longitude));
    }
}

在运行时,多边形数组不会被填充。

标签: javaandroid

解决方案


在尝试填充多边形之前,您应该检查数组的有效性。可能是引用数组中的值是空的。

private void DrawPolygon(List<LatLng> array) {

int length = array.size();
// Store a data object with the polygon, 
// used here to indicate an arbitrary type.
PolygonOptions poly = new PolygonOptions();

for(int i = 0; i < length; i++){
    if(array.get(i).latitude != null &&
        array.get(i).longitude != null){
        LatLng latLong = new LatLng(array.get(i).latitude,       
                              array.get(i).longitude);
        poly.add(latLong);
    }   
}

推荐阅读