Search code examples
androidcomputational-geometryarcgis

How to find Center Point from geometry arcgis android?


I am developing one application in that i want to show call out in center of geometry on my map I am new to arcgis.I tried so much but i am unable to get call out in center,please anybody help me how to solve this problem

my code

SimpleFillSymbol sfs = new SimpleFillSymbol(
                                        Color.RED);
                                sfs.setAlpha(5);
                                graphic = new Graphic(feature.getGeometry(),
                                        sfs, feature.getAttributes());
                                Polygon polygon = (Polygon) graphic
                                        .getGeometry();




                                int polygonpointscount=polygon.getPointCount();
                                if(polygonpointscount!=0)
                                {
                                    pointsize=polygonpointscount/2;
                                }
                                Point midpoint = polygon.getPoint(pointsize);
                                Callout callout = mMapView.getCallout();  
                                if (callout != null
                                        && callout.isShowing()) {
                                    callout.hide();
                                }
                                //Set the content, show the view  
                                callout.setContent(adminsearchloadView(governorate_Name,                              
                                area_Name,
                                        block_Number)); 
                                callout.setStyle(R.xml.calloutstyle);  
                                callout.setMaxHeight(100000);
                                callout.setMaxWidth(100000);
                                callout.refresh();
                                callout.show(midpoint); 

Solution

  • Short answer: use GeometryEngine.getLabelPointForPolygon(Polygon, SpatialReference).

    Long answer:

    From your code...

    int polygonpointscount=polygon.getPointCount();
    if(polygonpointscount!=0)
    {
        pointsize=polygonpointscount/2;
    }
    

    Polygon.getPointCount() returns the vertices of the polygon. For example, if the polygon is a rectangle, getPointCount() returns the corners. So your Callout will be at one of the corners instead of at the centroid.

    Instead, use GeometryEngine.getLabelPointForPolygon(Polygon, SpatialReference) . It doesn't guarantee to return the centroid, but it returns an interior point that is good for labeling (and it looks like the centroid to me). Make sure you pass a SpatialReference object that tells getLabelPointForPolygon what the spatial reference of your polygon is.

    If you must have the centroid, you'll need to create a geoprocessing service based on ArcGIS's Feature to Point tool. However, getLabelPointForPolygon is much easier to implement and faster to execute and probably satisfies your need.