How do i create polygon using mapviewhelper ?

1734
8
Jump to solution
08-01-2016 04:56 AM
MohamedEzzerI
New Contributor III

This is my code, but not working, is there any thing wrong !?

public void createPolygon() {

        arcMap.addLayer(graphicsLayer);

       String url = "http://.../polygons.json";

        final String resp = StudioUtilities.sendGetRequest(url);

        arcMap.setOnStatusChangedListener(new OnStatusChangedListener() {

        private static final long serialVersionUID = 1L;

        public void onStatusChanged(Object source, STATUS status) {

        try {

                 JSONObject jsonObject = new JSONObject(resp);

                 JSONArray jsonArray = jsonObject.getJSONArray("polygons");

                 

                 int count = 0;

                 while (count < jsonArray.length()) {

                 JSONObject polygon = jsonArray.getJSONObject(count);

                 JSONObject borderObject = polygon.getJSONObject("border");

                 JSONObject startPointObject = polygon.getJSONObject("starting_point");

                 final JSONArray pointsArray = polygon.getJSONArray("points");

                  double[][] p = {

                      {

                            33.599389, -7.610728
                       },

                       {

                            33.599985, -7.597180
                       },

                       {

                            33.588879, -7.617027
                       }

                  };
                  int loaded = mvHelper.addPolygonGraphic(p, "pol_t", "pol_st", "", Color.GREEN, Color.DKGRAY, 2, 0);

                  if (loaded < 0) {

                           Toast.makeText(ctx, "Marker Graphic not added to MapView", Toast.LENGTH_SHORT).show();

                  } else {

                           Toast.makeText(ctx, "Marker Graphic is added to MapView", Toast.LENGTH_SHORT).show();

                  }
                  count++;

              }

  } catch (JSONException e) {

            e.printStackTrace();

  }

  });

}

0 Kudos
1 Solution

Accepted Solutions
AlexanderNohe1
Occasional Contributor III

Create the color this way:

Color.argb(50, 49, 247, 10)

The first value is the alpha value which is showing 50% transparency.

Therefore, the line would look like this:

int graphic = mapViewHelper.addPolygonGraphic(latLon,"pol_t", "pol_st", "", Color.argb(50, 49, 247, 10), Color.DKGRAY,2, 0);

If this helped resolve the issue, would you kindly select this as the correct answer.

The attached screenshot is what I got below for the above snippet:

2016-08-01_10-46-3522.png

View solution in original post

8 Replies
AlexanderNohe1
Occasional Contributor III

Are you getting any errors or tracebacks when running?

0 Kudos
MohamedEzzerI
New Contributor III

No, but i need your help to fill the 2 dimensional array (double[][] p) from the points Array :

polygons: [

{

    • title: "t_poly1",
    • subtitle: "subt_poly1",
    • background: "green",
    • border: {
      • color: "darkgray",
      • size: 2,
      • style: "solid"
      },
    • points:
      [

      {

      latitude: "33.6060236",

      longitude: "-7.630058"

      },

      {

      latitude: "33.601975",

      longitude: "-7.632489"

      },

      {

      latitude: "33.605867",

      longitude: "-7.635220"

      },

    • {
    • latitude: "33.605626",

    • longitude: "-7.632988"

    • }

    • ]

]

0 Kudos
AlexanderNohe1
Occasional Contributor III

Hello Mohamed EzzerI​,

I was able to get the feature to draw in Casablanca using the following snippet:

package com.arcgis.androidsupportcases.addpolygongraphic;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.esri.android.map.MapView;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.toolkit.map.MapViewHelper;

public class MainActivity extends AppCompatActivity {

  MapView mapView;

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

   mapView = (MapView) findViewById(R.id.map);
   mapView.setOnStatusChangedListener(new OnStatusChangedListener() {
   @Override public void onStatusChanged(Object o, STATUS status) {
   if (status == STATUS.INITIALIZED) {
  MapViewHelper mapViewHelper = new MapViewHelper(mapView);
  double[][] latLon = new double[][] {{
   33.599389, -7.610728
   },
   {
   33.599985, -7.597180
   },
   {
   33.588879, -7.617027
   }
  };
  int graphic = mapViewHelper.addPolygonGraphic(latLon,"pol_t", "pol_st", "", Color.GREEN, Color.DKGRAY,2, 0);
   Log.e("NOHE", "" + graphic);
   }
  }
  });

  }
}

Here is the screenshot of my end result:

2016-08-01_10-46-35.png

0 Kudos
MohamedEzzerI
New Contributor III

^^ , it worked for me, filling the array statically , but what i need is to gather data from the json, if it is possible

0 Kudos
MohamedEzzerI
New Contributor III

Thnks Alexander, i hope that you could help me to solve this problem, i already tried to fill the p array using this code, but the app crashes :

double[][] p = new double[2][pointsArray.length()];

int pointCount = 0;

while (pointCount < pointsArray.length()) {

   try {

        final JSONObject point = pointsArray.getJSONObject(pointCount);

       
        p[0][pointCount] = Double.parseDouble(point.getString("latitude"));

        p[1][pointCount] = Double.parseDouble(point.getString("longitude"));

       } catch (JSONException e) {

            e.printStackTrace();

       }

       pointCount++;

}

0 Kudos
AlexanderNohe1
Occasional Contributor III

double[][] p = new double[pointsArray.length()][2];

The points array would be the outside array which would contain the amount of points.  I believe that you have this backwards.  As well as this:

p[pointCount][0] = Double.parseDouble(point.getString("latitude"));

p[pointCount][1] = Double.parseDouble(point.getString("longitude"));

0 Kudos
MohamedEzzerI
New Contributor III

Thx a lot, it worked ,
one more issue, is polygon FillStyle, i won't it solid, cuz it covers the map, is there any suggestion

0 Kudos
AlexanderNohe1
Occasional Contributor III

Create the color this way:

Color.argb(50, 49, 247, 10)

The first value is the alpha value which is showing 50% transparency.

Therefore, the line would look like this:

int graphic = mapViewHelper.addPolygonGraphic(latLon,"pol_t", "pol_st", "", Color.argb(50, 49, 247, 10), Color.DKGRAY,2, 0);

If this helped resolve the issue, would you kindly select this as the correct answer.

The attached screenshot is what I got below for the above snippet:

2016-08-01_10-46-3522.png