在Android中Google地图V2上,圆形上的经纬度点

5

我需要存储在谷歌地图上绘制的圆的所有LatLng点,如下所示:enter image description here

我有圆和半径(以米为单位)。如何获得?我尝试了以下代码......

 private ArrayList<LatLng> makeCircle(LatLng centre, double radius, float zoom)
 {
     ArrayList<LatLng> points = new ArrayList<LatLng>();
     LatLngBounds.Builder builder = new LatLngBounds.Builder();
      
     double EARTH_RADIUS = 6378100.0;

     for (double t = 0; t <= Math.PI * 2; t += 1.0)
     {
         double rad = radius + zoom * EARTH_RADIUS;
         double latPoint = centre.latitude + (rad / EARTH_RADIUS) * Math.sin(t);
         double lonPoint = centre.longitude + (rad / EARTH_RADIUS) * Math.cos(t) / Math.cos(centre.latitude);
         points.add(new LatLng(latPoint * 180.0 / Math.PI, lonPoint * 180.0 / Math.PI));
         Marker customMarker = map.addMarker(new MarkerOptions()
        .position(new LatLng(latPoint,lonPoint)));
        
         builder.include(new LatLng(latPoint,lonPoint));
        
         LatLngBounds bound = builder.build();
         CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bound, width-100, height-100, 20);
         map.animateCamera(cu);
     }
     return points;
 }

但我得到的分数不是在准确的位置上。我得到了这个问题。如何解决?

不确定是否有帮助。请查看此链接:http://math.stackexchange.com/questions/332743/calculating-the-coordinates-of-a-point-on-a-circles-circumference-from-the-radiu - Raghunandan
不行,不起作用。我不知道哪里出了计算错误。根据我的想法,如果在制作圆形时能得到精确的缩放级别,那么问题就可以解决。 - swati srivastav
不是显示红色标记绘制的不同圆形,而是我想要黑色圆圈标记的那个圆形。 - swati srivastav
这意味着你的圆圈里没有包含标记。 - Piyush
它具有距离的LatLng,因此无法获取精确位置,并且不会在其上添加标记。 - swati srivastav
显示剩余3条评论
1个回答

11
“缩放”因素在这里的计算中并不重要。按照下面所示,更新您的makeCircle()方法,它将完全按照您想要的方式工作:
private ArrayList<LatLng> makeCircle(LatLng centre, double radius) 
    { 
    ArrayList<LatLng> points = new ArrayList<LatLng>(); 

    double EARTH_RADIUS = 6378100.0; 
    // Convert to radians. 
    double lat = centre.latitude * Math.PI / 180.0; 
    double lon = centre.longitude * Math.PI / 180.0; 

    for (double t = 0; t <= Math.PI * 2; t += 0.3) 
    { 
    // y 
    double latPoint = lat + (radius / EARTH_RADIUS) * Math.sin(t); 
    // x 
    double lonPoint = lon + (radius / EARTH_RADIUS) * Math.cos(t) / Math.cos(lat);

    // saving the location on circle as a LatLng point
    LatLng point =new LatLng(latPoint * 180.0 / Math.PI, lonPoint * 180.0 / Math.PI);

    // here mMap is my GoogleMap object 
    mMap.addMarker(new MarkerOptions().position(point));

    // now here note that same point(lat/lng) is used for marker as well as saved in the ArrayList 
    points.add(point);

    } 

    return points; 
    }

我相信它对你有所帮助 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接