使用数组在Google Map上绘制多边形[]-Android [英] Drawing polygons on google Map using array[] - Android

查看:62
本文介绍了使用数组在Google Map上绘制多边形[]-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据数组列表在Google地图上绘制一个多边形.首先检查数组长度,然后手动添加点以绘制多边形.下面的代码可以正常工作.

i am trying to draw a polygon on the Google map depending on the array list. first check for the array length then i manually add points to draw the polygon. the below code works fine.

由于.add(new LatLng(array[0].a, array[0].b))是单行语句,所以我无法循环它.

since the .add(new LatLng(array[0].a, array[0].b)) is a single line statement i am unable to loop it.

我有什么办法可以循环执行此操作吗?

Is there any way i can do this in a loop.

private void drawpolygon(DistanceInfo  array[]) {

    int lengh = array.length;
    if(lengh==2){
        mMap.addPolygon(new PolygonOptions()
            .add(new LatLng(9.6632139, 80.0133258))
        .add(new LatLng(array[0].a, array[0].b))
        .add(new LatLng(array[1].a, array[1].b))
       .fillColor(Color.GRAY)); 
    }
    else if(lengh==4){
        mMap.addPolygon(new PolygonOptions()

        .add(new LatLng(9.6632139, 80.0133258))
        .add(new LatLng(array[0].a, array[0].b))
        .add(new LatLng(array[1].a, array[1].b))
        .add(new LatLng(array[2].a, array[2].b))
        .add(new LatLng(array[3].a, array[3].b))
       .fillColor(Color.GRAY));
    }
}

推荐答案

尝试

private void drawpolygon(DistanceInfo  array[]) {

    int length = array.length;

    // Optional length checks. Modify yourself.
    if(length == 0) 
    {
       // Do whatever you like then get out. Do not run the following.
       return;
    }

    // We have a length of not 0 so...    
    PolygonOptions poly = new PolygonOptions();
    poly.fillColor(Color.GRAY);

    // Initial point
    poly.add(new LatLng(9.6632139, 80.0133258);

    // ... then the rest.
    for(int i = 0; i < length; i++)
    {
        poly.add(new LatLng(array[i].a, array[i].b));
    }

    // Done! Add to map.
    mMap.addPolygon(poly);
}

请注意,这允许array []具有任意长度(0除外).请添加您自己的代码以进行长度检查:)

Note that this allows array[] to be of any length (except 0). Please add your own code for length checking :)

这篇关于使用数组在Google Map上绘制多边形[]-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆