简体   繁体   中英

Add markers on the circumference of a circle in google maps

I'm trying to add markers around the circumference of a circle with a given radius. My centre point is the current location on my map and I'm trying to add 10 waypoints around me. I'm trying to use Pythagoras method for finding points on a circle, however, I don't think my calculations are correct here.

Here's my function:

private void drawCirclePoints()
    {    
        int numOfPoints = 10;
        int radius = 100;
        LatLng center = new LatLng(homeBase.latitude, homeBase.longitude);
        double slice = 360/numOfPoints;
        ArrayList<LatLng> centerArray = new ArrayList<>();

        for (int i = 0; i < numOfPoints; i++)
        {
            double angle = slice * i;
            double X = center.latitude + radius * Math.cos(angle * Math.PI / 180);
            double Y = center.longitude + radius * Math.sin(angle * Math.PI/180);
            centerArray.add(new LatLng(X, Y));
        }

        CircleOptions circleOptions = new CircleOptions();
        circleOptions.center(homeBase);
        circleOptions.radius(radius);
        circleOptions.fillColor(0x33FF0000);
        circleOptions.strokeColor(Color.BLUE);
        circleOptions.strokeWidth(3);  

        for (int i = 0; i < centerArray.size(); i++)
        {
            setMarker("sample", centerArray.get(i).latitude, centerArray.get(i).longitude);

        }

       markercircle = mMap.addCircle(circleOptions);

    }

My issue here is the coordinates returned aren't correct or what I expected. What am I doing wrong?

I'm not sure what you are expecting. Have you tried truncating results to few decimals or printing the results? If yes please do share!

I have slightly modified your code and results were as expected.

    int numOfPoints = 4;
    int radius = 100;

    LatLng center = new LatLng(0.0, 0.0);

    double slice = 360 / numOfPoints;
    ArrayList<LatLng> centerArray = new ArrayList<>();

    for (int i = 0; i < numOfPoints; i++)
    {
        double angle = slice * i;
        double X = center.latitude + radius * Math.cos(angle * Math.PI / 180);
        double Y = center.longitude + radius * Math.sin(angle * Math.PI / 180);

        DecimalFormat df = new DecimalFormat("#.##");
        centerArray.add(new LatLng(Double.parseDouble(df.format(X)), Double.parseDouble(df.format(Y))));
    }

    for (int i = 0; i < centerArray.size(); i++)
    {
        System.out.println("point " + (i + 1) + " = X:" + centerArray.get(i).latitude + ", Y: " + centerArray.get(i).longitude);

    }


point 1 = X:100.0, Y: 0.0
point 2 = X:0.0, Y: 100.0
point 3 = X:-100.0, Y: 0.0
point 4 = X:-0.0, Y: -100.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