简体   繁体   中英

Android maps v2, polygon error

I am having trouble adding my polygon with holes to my google map.

It was working with polylines (and I think once with polygon) but now it doesn't, whether the holes logic is there or not.

Here is all the code and it happens after the map is setup.

 01-25 16:22:53.921: E/AndroidRuntime(22986): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 01-25 16:22:53.921: E/AndroidRuntime(22986): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257) 01-25 16:22:53.921: E/AndroidRuntime(22986): at java.util.ArrayList.get(ArrayList.java:311) 01-25 16:22:53.921: E/AndroidRuntime(22986): at maps.y.bb.a(Unknown Source) 01-25 16:22:53.921: E/AndroidRuntime(22986): at maps.y.bb.c(Unknown Source) 01-25 16:22:53.921: E/AndroidRuntime(22986): at maps.y.bg.a(Unknown Source) 01-25 16:22:53.921: E/AndroidRuntime(22986): at maps.y.bg.b(Unknown Source) 01-25 16:22:53.921: E/AndroidRuntime(22986): at maps.y.ae.addPolygon(Unknown Source) 01-25 16:22:53.921: E/AndroidRuntime(22986): at com.google.android.gms.maps.internal.IGoogleMapDelegate$Stub.onTransact(IGoogleMapDelegate.java:152) 
try {

        JSONObject boundaries = new JSONObject("{\"type\":\"MultiPolygon\",\"coordinates\":[[[[-80.735555859,35.0606584407],[-80.7297754512,35.0651878022],[-80.729139294,35.0656818503],[-80.7289250745,35.065900852],[-80.7286221912,35.0660671391],[-80.7282894137,35.066312794],[-80.7275260823,35.0669496861],[-80.7230724393,35.0660242895],[-80.7227950616,35.0661147593],[-80.7218902721,35.0659176785],[-80.722176045,35.0657643995],[-80.720961508,35.0641575183],[-80.7209758716,35.0629317403],[-80.7272283465,35.0628131662],[-80.726641114,35.061423483],[-80.7265854346,35.061015353],[-80.7266510423,35.0606220672],[-80.7235677631,35.0606937582],[-80.7234837301,35.0595787025],[-80.7255096391,35.0597210677],[-80.7270440474,35.0598206983],[-80.7271985668,35.0591829945],[-80.7274183452,35.0587709241],[-80.7277152978,35.0585124776],[-80.7284566533,35.0579768923],[-80.727780986,35.0597106844],[-80.7299847264,35.0599581722],[-80.735555859,35.0606584407]]],[[[-80.7200742118,35.0674134672],[-80.7186999337,35.0705562642],[-80.7164176998,35.069933693],[-80.7150980153,35.0696170679],[-80.7157433595,35.0657510316],[-80.7152719628,35.0657629702],[-80.7150541804,35.0646523049],[-80.7155138273,35.0644640927],[-80.7169133321,35.0639508082],[-80.7172970965,35.0637601058],[-80.7175728371,35.0635834077],[-80.7183107407,35.0631329539],[-80.7184931359,35.0630502037],[-80.7186006099,35.0648619253],[-80.7185879713,35.0648540961],[-80.7186006849,35.0648654865],[-80.7200742118,35.0674134672]]],[[[-80.6892017218,35.0865938467],[-80.6836074596,35.0872774004],[-80.6836037958,35.0872706912],[-80.683607554,35.087262573],[-80.6838084759,35.0843125317],[-80.6838093023,35.0838010413],[-80.6907731212,35.082005808],[-80.6906868717,35.0819147663],[-80.6914540109,35.0819508048],[-80.6923350028,35.0819883607],[-80.6922022291,35.0818062139],[-80.6906081064,35.081775524],[-80.6892627084,35.0801975391],[-80.6880259464,35.0787334166],[-80.6876700467,35.0784015634],[-80.6863873389,35.0787500748],[-80.6833236198,35.0795693995],[-80.682052189,35.0799145893],[-80.6813631325,35.078511382],[-80.6824880399,35.0773168152],[-80.6831273312,35.0766747942],[-80.6839730169,35.0757398452],[-80.6847449558,35.0749088381],[-80.6851125671,35.0745232176],[-80.6868562873,35.0762921663],[-80.6869385663,35.0762348787],[-80.6880385407,35.075857727],[-80.6893815702,35.0756767665],[-80.6893041844,35.0757870067],[-80.6895418941,35.0757244239],[-80.6910892479,35.0753347457],[-80.6953548167,35.074308825],[-80.6947217548,35.0726824065],[-80.6938249008,35.0708171642],[-80.6928755869,35.0707476456],[-80.6926063661,35.0700471051],[-80.6965836865,35.0702385147],[-80.6981317926,35.0702759198],[-80.6993134907,35.0702479401],[-80.699303851,35.0720088657],[-80.7044604692,35.0700231034],[-80.7059676219,35.0694258442],[-80.7064318242,35.070114517],[-80.7062358632,35.0718825479],[-80.7070931359,35.0713564697],[-80.7099609034,35.0704230355],[-80.7099936802,35.0705152827],[-80.7100080651,35.0705503962],[-80.7099495884,35.0705908523],[-80.7100187353,35.0707057082],[-80.7100167527,35.0707880413],[-80.7100972756,35.0709149296],[-80.7101379822,35.0710881063],[-80.7102138409,35.0711693359],[-80.7101846935,35.071247158],[-80.7102411258,35.0714097659],[-80.7103023493,35.0715003479],[-80.710389265,35.0715783724],[-80.707476454,35.073601],[-80.7077178913,35.0739755754],[-80.7087496883,35.0733147365],[-80.7090951077,35.0738737844],[-80.7115336827,35.0724004396],[-80.7115289543,35.0723517336],[-80.7119904436,35.0722567899],[-80.7119402102,35.0721599562],[-80.7120096157,35.0721101991],[-80.712379284,35.0734705994],[-80.71269176,35.0745787999],[-80.7130325591,35.0757993846],[-80.710920954,35.0773077532],[-80.7093642515,35.0784028049],[-80.7099841194,35.080555276],[-80.7091997899,35.0811729934],[-80.7086062698,35.0816605739],[-80.706153571,35.0835724102],[-80.7052694052,35.0842067533],[-80.7034908473,35.0856980884],[-80.7023961873,35.0865548562],[-80.7013399763,35.087478134],[-80.7000482398,35.0884900711],[-80.6986890109,35.0896462123],[-80.6963494752,35.0914593015],[-80.6948488535,35.0925786583],[-80.694848095,35.0926560399],[-80.6944683903,35.0943039003],[-80.6928489794,35.0942189034],[-80.6912333698,35.0954877374],[-80.6814301091,35.1033733546],[-80.6759910014,35.1036684268],[-80.676284034,35.104210002],[-80.6762103702,35.1042354101],[-80.67719044,35.1057185102],[-80.6775701585,35.1063259403],[-80.6754613322,35.1076781632],[-80.6748644376,35.1080552826],[-80.673594298,35.1090666249],[-80.6719077296,35.1103946423],[-80.6703790777,35.1116472926],[-80.6699256895,35.1112450928],[-80.6696705955,35.112193583],[-80.6690859143,35.1120827809],[-80.668908562,35.1126613551],[-80.6680473027,35.1124955642],[-80.6678832061,35.1131327573],[-80.6677639689,35.1131069686],[-80.667556535,35.1131220256],[-80.667453441,35.1131600278],[-80.666459359,35.1137102408],[-80.6663306762,35.1137668849],[-80.6660559502,35.1139383296],[-80.6655854342,35.1143319477],[-80.6654890957,35.1145192204],[-80.6650770434,35.1148693555],[-80.6647153446,35.1145700964],[-80.6636426861,35.115071222],[-80.6625216752,35.1149015024],[-80.6626687137,35.1153811044],[-80.6598474402,35.1158375148],[-80.6596848129,35.1158671841],[-80.6595513601,35.1158720659],[-80.6594381864,35.1159620202],[-80.6591858843,35.1160534039],[-80.6590844379,35.1160796643],[-80.6590512663,35.1164550566],[-80.6589623174,35.1175536532],[-80.6576318529,35.1196294949],[-80.65837002,35.1201710997],[-80.6586751564,35.1203985787],[-80.6591171649,35.1207887831],[-80.6573152693,35.1223071643],[-80.6559980189,35.1233951662],[-80.655926363,35.1229724408],[-80.6543961779,35.1236243941],[-80.653936493,35.1238257769],[-80.6544684451,35.1244433862],[-80.6537987198,35.1249241966],[-80.6545133901,35.1253785865],[-80.6544374132,35.1258399141],[-80.6545245198,35.1264758071],[-80.6545057883,35.1268327097],[-80.6544390845,35.1272024624],[-80.6543245293,35.1274082647],[-80.6540923877,35.1278534401],[-80.6521348557,35.1303096848],[-80.6519784991,35.1304672832],[-80.6515332991,35.1306532164],[-80.6508635639,35.1305617764],[-80.6510742908,35.1297907369],[-80.6492576629,35.1296254181],[-80.6491159372,35.1303783613],[-80.6489262629,35.1303565644],[-80.6486160268,35.130427859],[-80.6482488539,35.1304359167],[-80.6480056476,35.1303355933],[-80.6478007046,35.1302926647],[-80.6479423629,35.1316106271],[-80.6479541402,35.1320097873],[-80.6480408806,35.1328133425],[-80.6482554664,35.1346180295],[-80.6470441676,35.1358233647],[-80.6463329014,35.1364335631],[-80.6451999232,35.1372933599],[-80.6440387412,35.1380437921],[-80.6436400263,35.1383296551],[-80.6429905796,35.1386716625],[-80.6424832884,35.1389336953],[-80.6410595471,35.1395474472],[-80.6390951481,35.1404032318],[-80.636846804,35.1396868888],[-80.6369454847,35.1399812312],[-80.6366720154,35.1402226986],[-80.6364154014,35.1400097489],[-80.6359141204,35.1398092553],[-80.6349108935,35.1419657481],[-80.6331199086,35.142221632],[-80.6307550215,35.1425431512],[-80.6271192845,35.1438968064],[-80.6262975903,35.1445632386],[-80.6257007584,35.1442237695],[-80.6250678081,35.1437476136],[-80.6253059934,35.1458294043],[-80.6247483725,35.1459679766],[-80.6181058949,35.1473007779],[-80.616714438,35.1445058826],[-80.6169541909,35.1440576332],[-80.6164045472,35.1440111621],[-80.6154895842,35.1439339543],[-80.6131068476,35.1437341183],[-80.6101234567,35.1435605288],[-80.6092556672,35.1471629009],[-80.6091536233,35.1476336869],[-80.6090208182,35.1480557266],[-80.6089834481,35.1484132505],[-80.6081508699,35.147973195],[-80.6075467125,35.1476386645],[-80.6048243778,35.1465908449],[-80.6047790672,35.1463628297],[-80.604830461,35.1461457235],[-80.6045458635,35.1452134138],[-80.6042387892,35.144178329],[-80.6041107731,35.143707553],[-80.6039535639,35.1434505408],[-80.603738135,35.1432522167],[-80.603508526,35.1430876116],[-80.6032792774,35.14294129],[-80.6030681875,35.1414535074],[-80.6054221338,35.1412332319],[-80.6057067049,35.1407021431],[-80.6060043535,35.1399879499],[-80.6068787989,35.1389795543],[-80.6071862095,35.1378201868],[-80.6074794177,35.1372584603],[-80.6077353061,35.1371256059],[-80.6070479543,35.1364644933],[-80.6068909373,35.1363668733],[-80.6057893033,35.1352127248],[-80.6029728961,35.1325801749],[-80.602705096,35.1323344583],[-80.6021254634,35.1316608107],[-80.6020665308,35.1314067844],[-80.6018804088,35.1312394044],[-80.6040100378,35.130098547],[-80.6038938339,35.1300364902],[-80.6040276017,35.1277847053],[-80.6040878524,35.1263207538],[-80.6052310561,35.1263794037],[-80.606053778,35.1264836272],[-80.606318985,35.1263856062],[-80.6066873314,35.1262496344],[-80.6080065615,35.1266028432],[-80.6086022449,35.1267223182],[-80.6094835494,35.1269653902],[-80.6102056703,35.1270000828],[-80.610401148,35.1269974774],[-80.6104557288,35.1270299854],[-80.6104888856,35.1270018473],[-80.6107224002,35.1268768693],[-80.6107866673,35.1267662261],[-80.6108596867,35.1266590058],[-80.6113987261,35.1263618544],[-80.61182941,35.1262583205],[-80.6119618128,35.1261346889],[-80.6120228691,35.1258125932],[-80.6122644334,35.1257539767],[-80.6125437509,35.1258998112],[-80.6130321056,35.1258877032],[-80.6132009225,35.1257940021],[-80.6133242036,35.1256490887],[-80.6131832249,35.1254619803],[-80.6128174025,35.1251620408],[-80.6126767098,35.1251225311],[-80.61220444,35.1251066773],[-80.6119187772,35.1246396464],[-80.6124097945,35.1242398026],[-80.6129591106,35.1240607512],[-80.6137266923,35.123668288],[-80.6162220971,35.1250982529],[-80.6178098333,35.1260219576],[-80.6183533599,35.1251794602],[-80.6190645012,35.125607648],[-80.6197124513,35.1246240414],[-80.6206533288,35.1234537047],[-80.620796212,35.1235768684],[-80.6209232451,35.123804601],[-80.6211312179,35.1240052236],[-80.6214593422,35.1243181034],[-80.6216179279,35.1245991957],[-80.6218216172,35.1247671638],[-80.6222746458,35.1253097778],[-80.6222800161,35.1242239969],[-80.6247588564,35.1264784305],[-80.6286342888,35.129783087],[-80.629231252,35.1303012757],[-80.6285596039,35.1307700951],[-80.629232723,35.1317248641],[-80.6300965542,35.1314251678],[-80.6304259751,35.1313764086],[-80.631720515,35.1303618525],[-80.6263403428,35.1266676468],[-80.6250187491,35.1266632718],[-80.6248402938,35.1265050315],[-80.6255871257,35.1261717858],[-80.6258011083,35.1259991058],[-80.6260958141,35.1259009705],[-80.6271099154,35.1265286316],[-80.6277576304,35.1267371894],[-80.628316877,35.1267185756],[-80.6287156765,35.1267685935],[-80.628839722,35.1269015345],[-80.6288131356,35.1265844815],[-80.6292198462,35.1263869942],[-80.6327543271,35.1291236937],[-80.6335897433,35.1277441849],[-80.6345684319,35.1262770172],[-80.634765339,35.1260071027],[-80.6349591975,35.1260532515],[-80.6352616501,35.125409026],[-80.6385080022,35.1189031646],[-80.6402014828,35.1201713528],[-80.6424907125,35.1189716885],[-80.6435776766,35.1197556695],[-80.6443066356,35.1206406618],[-80.6459343705,35.1196718123],[-80.6467364979,35.1187512376],[-80.6482785997,35.1170384237],[-80.6491544947,35.1161394259],[-80.6502687258,35.1151030423],[-80.6486109449,35.1135928039],[-80.6484216813,35.113580595],[-80.6474325392,35.1127361549],[-80.6494051931,35.1112923165],[-80.6458633831,35.1085714216],[-80.6469791865,35.1076686943],[-80.6478608876,35.1085441878],[-80.6509350524,35.106571857],[-80.6508577061,35.1023054281],[-80.6524447116,35.1040355329],[-80.6527295103,35.1040770564],[-80.6529680841,35.1043030671],[-80.6536734734,35.104027156],[-80.6538165704,35.1040076119],[-80.6540852486,35.1039179738],[-80.6543176756,35.1038422393],[-80.6544863377,35.1037454328],[-80.6546198957,35.1037466516],[-80.6550585129,35.1034266708],[-80.6552715344,35.1038351586],[-80.655424781,35.1034040908],[-80.6558537943,35.103257686],[-80.6531375935,35.1002329504],[-80.6558241711,35.0986947604],[-80.6582037743,35.1015391579],[-80.659808642,35.1004412445],[-80.6604638868,35.1000146248],[-80.6606004278,35.0987153267],[-80.6620809142,35.0988085121],[-80.6599137372,35.0940235148],[-80.6616431043,35.0941032774],[-80.6645679109,35.0962645546],[-80.6652546605,35.0955771128],[-80.6651360483,35.0954973917],[-80.6660875657,35.0945005986],[-80.6662583415,35.0944908467],[-80.66624406,35.0942321831],[-80.6658371196,35.0941342526],[-80.6653452092,35.0938576804],[-80.6640449178,35.0927389252],[-80.6647990725,35.0926545708],[-80.6653905431,35.0925354757],[-80.6652608264,35.0923523645],[-80.6644112773,35.0894675908],[-80.6643501956,35.0891772331],[-80.663760707,35.0893924445],[-80.6634576712,35.0890933822],[-80.6618944362,35.0896548148],[-80.6618970647,35.0889003845],[-80.6645597387,35.0875398247],[-80.66420213,35.087211932],[-80.6691760904,35.0847484365],[-80.6696726507,35.085442728],[-80.6709826768,35.0848255285],[-80.672128557,35.0884928908],[-80.6724163066,35.0893616379],[-80.6725687255,35.0897737047],[-80.6730024371,35.0903002109],[-80.6737959865,35.0908217289],[-80.6745594276,35.0889991181],[-80.6743168505,35.0890172734],[-80.6734985388,35.08641042],[-80.6726886162,35.0842102278],[-80.6727787015,35.0841460367],[-80.6728751551,35.0840705594],[-80.6731138588,35.0838863072],[-80.6740783858,35.0830963554],[-80.6748141873,35.0825610337],[-80.677037018,35.0843348163],[-80.6780681496,35.0832850466],[-80.679427234,35.0850559943],[-80.6802450053,35.0858803711],[-80.6823594712,35.0876417449],[-80.6826231292,35.0875476273],[-80.6835960451,35.0872564961],[-80.6836037958,35.0872706912],[-80.6836002885,35.0872782686],[-80.6836074596,35.0872774004],[-80.6836078757,35.0872781628],[-80.6848441775,35.088901394],[-80.684475276,35.0892277736],[-80.684845611,35.0895220099],[-80.6854648603,35.0890081273],[-80.6857619008,35.0892441263],[-80.686209588,35.08960901],[-80.6867264599,35.090016589],[-80.6867937079,35.0899657491],[-80.6860720548,35.0893801412],[-80.6864075202,35.08908853],[-80.6872383391,35.0885311397],[-80.6880120798,35.0892501202],[-80.6882381911,35.0891752272],[-80.68741128,35.0884538733],[-80.6873768186,35.0884387606],[-80.6877818432,35.0882085487],[-80.688163848,35.0879661825],[-80.6891520051,35.087579713],[-80.6892316314,35.0877305668],[-80.6895925319,35.0876102323],[-80.6892017218,35.0865938467]],[[-80.6704378421,35.0948125638],[-80.6696999375,35.0941296997],[-80.669010735,35.0946183779],[-80.6690369083,35.0947730874],[-80.6689746217,35.0948393319],[-80.6689111304,35.0948467468],[-80.6688483088,35.0948868457],[-80.6679248845,35.0960242033],[-80.6690029079,35.0966108409],[-80.6704378421,35.0948125638]],[[-80.6536903079,35.1210434418],[-80.6531144541,35.1204897415],[-80.6542740949,35.1190577051],[-80.6552325817,35.1176718766],[-80.6568109412,35.1175709856],[-80.6567556021,35.1170382979],[-80.6576835964,35.1166994032],[-80.6579848071,35.116552001],[-80.6586091661,35.1162416525],[-80.6587330338,35.1161302139],[-80.659113413,35.1159148453],[-80.6592276846,35.1142285276],[-80.6572502348,35.1148905045],[-80.659189946,35.112373381],[-80.6573293287,35.1114710906],[-80.654794954,35.1139719152],[-80.6512735975,35.1176337593],[-80.6486244515,35.1203557654],[-80.6486476433,35.1215030273],[-80.6489461204,35.123037198],[-80.6498235911,35.1223415542],[-80.6526486774,35.1241726121],[-80.6523421052,35.1246066092],[-80.6528906852,35.1249466072],[-80.6532924569,35.1244503397],[-80.65281726,35.1240697132],[-80.6520579506,35.1235832327],[-80.6514432342,35.1230763396],[-80.6536903079,35.1210434418]],[[-80.617690965,35.1427690358],[-80.6227869511,35.1416408722],[-80.6214124412,35.1407188194],[-80.6209892347,35.1385146979],[-80.6203344141,35.1350671264],[-80.6216818774,35.1351907031],[-80.6227009082,35.1361402694],[-80.6238353171,35.1359550455],[-80.6237929422,35.1355589835],[-80.6207782633,35.1331063498],[-80.622164895,35.1299996851],[-80.6221723268,35.1286397069],[-80.6212307912,35.1281140592],[-80.6178975387,35.1321817104],[-80.6162974308,35.136707716],[-80.6175013245,35.1417628101],[-80.617690965,35.1427690358]],[[-80.6172865761,35.1420216347],[-80.6172860889,35.1420203672],[-80.6172853756,35.1420203768],[-80.6172865761,35.1420216347]]]]}");
        JSONArray coordinates = boundaries.getJSONArray("coordinates");


        for(int i = 0; i < coordinates.length(); i++){
            try {
                JSONArray currentPerimeter = coordinates.getJSONArray(i).getJSONArray(0);

                ArrayList<LatLng> holesArray = new ArrayList<LatLng>();
                ArrayList<ArrayList<LatLng>> holesHolder = new ArrayList<ArrayList<LatLng>>();
                ArrayList<LatLng> mainArray = new ArrayList<LatLng>();


                for(int j = 0; j < currentPerimeter.length(); j++){ //this just adds outlines and outlying objects, but no holes.
                    mainArray.add(new LatLng(currentPerimeter.getJSONArray(j).getDouble(1), currentPerimeter.getJSONArray(j).getDouble(0)));

                }
                JSONArray holes = coordinates.getJSONArray(i);
                if(holes.length()>0)
                {

                    //new for loop to make holes
                    for(int j = 1; j < holes.length(); j++)
                    {
                        holesArray.add(new LatLng(holes.getJSONArray(j).getDouble(1), holes.getJSONArray(j).getDouble(0)));
                    }
                }

                mMap.addPolygon(new PolygonOptions().strokeWidth(3).strokeColor(0xFFEE8888).addAll(mainArray).addHole(holesArray));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

The problems seems to be that addHole does not guard against a zero length array for holes. So, you should check to make sure you do not pass it a zero length array, because you are passing a zero-length array. Putting that guard in and logging your caught exceptions reveals your parsing is going awry in for your holes.

Specifically,

holesArray.add(new LatLng(holes.getJSONArray(j).getDouble(1), holes.getJSONArray(j).getDouble(0)));

is throwing --

org.json.JSONException: Value [-80.6696999375,35.0941296997] at 1 of type org.json.JSONArray cannot be converted to double

So, you are trying to parse an array as a double . I think your problem is that this line --

JSONArray holes = coordinates.getJSONArray(i);

should be something else. My previous suggestion got it to work, but I don't think it was semantically correct, because it was repeating the same array as above. It is not clear from your JSON where holes should be, but that it line is where you should start to look. Perhaps something like,

if (coordinates.getJSONArray(i).length() > 1) {
   JSONArray holes = coordinates.getJSONArray(i).getJSONArray(1);
   if (holes.length() > 0) {
       // new for loop to make holes
       for (int j = 1; j < holes.length(); j++) {
       holesArray.add(new LatLng(holes.getJSONArray(j).getDouble(1), holes.getJSONArray(j).getDouble(0)));
       }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM