5

我需要存储在谷歌地图上绘制的所有 LatLng 点。像 : 在此处输入图像描述

我有圆和半径(以米为单位)。怎么得到?。我试过用代码......

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

但我得到了积分,但不是在确切的位置。我得到这个

在此处输入图像描述

如何解决这个问题?

4

1 回答 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; 
    }

我相信它对你有帮助:)

于 2013-09-06T10:04:04.600 回答