Select to view content in your preferred language

Draw circle with centre point and clear circle when user change radius

1637
3
09-08-2020 03:30 AM
KalpitMistry
Emerging Contributor

We had developer an application in past which had integrated old Arc gis  core Android library. Now we have migrated from 10x to 100.9.0 Runtime library. And we have observed many functionality is not working due to method changed. Most of the functionality we had fixed but we are stuck at 2 functionality which is like draw circle with user centre location.

Please see my below code which I have used to draw circle.

Actually we are able to get the circle on map but it is not with the centre point which I have used. I have passed my location as param you can see in below method, the first one param.

private Graphic getCircleForMapReference(LatLng position, double radius, SpatialReference mapReference, MapView mapView) {
    int pointCount = 360;

    if (radius > 0)
    {
        Application application = ApplicationUtil.getInstance().getApplicationInstance();
        int color = application.getResources().getColor(com.cityshob.responder.R.color.investigationCircleOrange);
        Point center = LocationUtils.convertToMapPoint(position, mapReference);
        SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID,color, 7);
        PointCollection pointCollection = new PointCollection(SpatialReferences.getWgs84());


        for (int i = 0; i <= pointCount; i++)
        {
            double rad = 2 * Math.PI / pointCount * i;
            double x = Math.cos(rad) * radius + center.getX();
            double y = Math.sin(rad) * radius + center.getY();

            android.graphics.Point screenPoint = new android.graphics.Point((int) Math.round(x),(int) Math.round(y));
            Point mapPoint = mapView.screenToLocation(screenPoint);
            Point wgs84Point = (Point) GeometryEngine.project(mapPoint, SpatialReferences.getWgs84());
            pointCollection.add(wgs84Point.getX(),wgs84Point.getY());
        }
        Polyline lineGeometry = new Polyline(pointCollection);
        Graphic lineGraphic = new Graphic(lineGeometry, lineSymbol);
        return lineGraphic;
    }

    return null;
}

Second issue which I am facing is to clear that circle. I used below line of code to clear but it is not working.
mGraphicsLayer.getGraphics().remove(circle)
where 
mGraphicsLayer is the object of GraphicOverlay
circle is the index where I have added object.
we are stuck on above 2 points, Please help us as soon as possible. Please let us know what we had implemented wrong.
0 Kudos
3 Replies
by Anonymous User
Not applicable

Hello! Adding a planar buffer as a graphic sounds like it'll do exactly what you're looking for. See this sample: arcgis-runtime-samples-android/java/buffer at master · Esri/arcgis-runtime-samples-android · GitHub 

To your second point, `mGraphicsLayer.getGraphics()` returns a list, so you should be correct in calling remove() on it. Are you sure the index isn't out by one?

If the circle is the only graphic in the graphicsOverlay you can call graphicsOverlay.getGraphics().clear() like here: arcgis-runtime-samples-android/MainActivity.java at master · Esri/arcgis-runtime-samples-android · G... 

Let me know if this helps!

Thanks,

Trevor

0 Kudos
KalpitMistry
Emerging Contributor

Hi Thanks for the reply.
Can you please help me to draw circle with dynamic radius. I have my current lat/long which is centre point and through that centre point i need to draw circle for that I need to calculate x and y coordinates.  I have used below equation to calculate x and y

double rad = 2 * Math.PI / pointCount * i;
double x = Math.cos(rad) * radius + center.getX();
double y = Math.sin(rad) * radius + center.getY();

after that convert x,y to the map location using mapToLocation method, it will return me object of Point which I am adding into PointCollection object.

Please suggest me what I did wrong. 

0 Kudos
KalpitMistry
Emerging Contributor

In example it shows the conversion from touch event to map location, that is working fine and we have no question on that. The problem which we are facing in  below method

public static Point convertToMapPoint(double longitude, double latitude,
                                      SpatialReference mapReference) {
    Point wgsPoint = new Point(longitude, latitude);
    return (Point) GeometryEngine.project(wgsPoint,mapReference);
}
For example if we have location value "-102.29872652008582, 21.879840237531294" then with help of above method I need to convert location to mapView point which is near to [-1.1387842145016305E7, 2497104.718795404] as value.

Could you please suggest what we can do in that case

0 Kudos