android - Android 中 Google Map V2 上圆圈上的 LatLng 点

标签 android google-maps

我需要存储在谷歌地图上绘制的圆的所有 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;
 }

但我得到了分数,但不是在确切的位置。我明白了

enter image description here

如何解决?

最佳答案

“缩放”因子与此处的计算无关。如下所示更新您的 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; 
    }

我相信它对你有帮助:)

关于android - Android 中 Google Map V2 上圆圈上的 LatLng 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18650907/

相关文章:

android - 从 Android java 应用程序创建符号链接(symbolic link)

android - 我可以使工具栏的颜色渐变吗?

android - 喷气背包导航 : Hide label in toolbar

android - Google Maps V2 Android 作为 fragment 工作,但将白屏(带缩放控制)显示为子 fragment

javascript - React-Native 选择器警告

android - 如何在android中访问数据库sqlite

java - 无法定位当前 GPS 位置

android - 从相机位置获取 LatLngBounds

java - 保存 Google map 的部分内容

android - 是否可以在谷歌地图中的标记下方添加文本?