在Google地图中的圆周上添加标记

时间:2017-11-28 11:47:02

标签: java google-maps android-studio latitude-longitude geometry

我试图在给定半径的圆周上添加标记。我的中心点是我地图上的当前位置,我试图在我周围添加10个航点。我试图用毕达哥拉斯方法在圆上找点,但是,我不认为我的计算是正确的。

这是我的功能:

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);

    }

我的问题是返回的坐标不正确或我的预期。我究竟做错了什么?

1 个答案:

答案 0 :(得分:0)

我不确定你在期待什么。您是否尝试将结果截断为几位小数或打印结果?如果是,请分享!

我稍微修改了您的代码,结果与预期一致。

    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);

    }

结果:
点1 = X:100.0,Y:0.0
点2 = X:0.0,Y:100.0
点3 = X:-100.0,Y:0.0
第4点= X:-0.0,Y:-100.0

相关问题