Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LatLng Points on circle on Google Map V2 in Android

I need to store all the LatLng points of circle drawn on google map. like : enter image description here

I have circle and radius(in meter). How to get that?. i tried with the code......

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

but i m getting points but not on exact locations. i am getting this

enter image description here

How to solve this?

like image 682
swati srivastav Avatar asked Sep 06 '13 05:09

swati srivastav


1 Answers

The 'zoom' factor is not relevant for the calculations here. Update your makeCircle() method as shown below and it will work exactly the way you want:

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

I am sure it helped you :)

like image 161
AnniJais Avatar answered Sep 18 '22 07:09

AnniJais