0

I have a GoogleMap in my project. It's set in zoom level 18. I want to draw a line that is 1 meter in length. I saw and use a code Like this:

googleMap.addCircle(new CircleOptions()
     .center(latLng1)
     .radius(5)
     .fillColor(Color.BLUE));     

I gave it's radius in meters. how can I do it with a line?(polyLine doesn't have this options) a line with specific LatLng and specific direction(for example: Heading from north) and specific length? I can specify direction by sin and cos.. but what can I do for length of the line?

jion
  • 127
  • 13

3 Answers3

2

For given point there is only one circle with given radius. But with lines the situation is a bit different. For given point there are infinite number of lines starting from this points and given length. Therefore you can't simple draw such line.

One way to do it is to pick a point on the circle with radius 1 meter and center your point. Here is a good example how to calculate point on a circle with given radius. Than just draw a line between the two points.

UPDATE:

This may help you how to find the LatLng points on the circle LatLng Points on circle on Google Map V2 in Android

Community
  • 1
  • 1
Mojo Risin
  • 8,136
  • 5
  • 45
  • 58
  • If I specify the direction... it just has a one line to draw. one point, a length and direction(degree)...I'm going to try your suggestion page – jion Feb 03 '15 at 08:37
  • problem is that: How can I specify 1 meter? for example: LatLng2.longitude=r x cos(a); and LatLng2.latitude=r x sin(a); but how can I explain to googlemap, that "r" is 1 meter? – jion Feb 03 '15 at 08:49
  • thank you a lot ... I used it but there is a problem. I explained it here : http://stackoverflow.com/questions/28321738/android-google-map-heading-is-drawn-wrongly-when-i-draw-a-line-with-specific – jion Feb 04 '15 at 12:45
1

To compute line end I use:

    SphericalUtil.computeOffset(LatLng,lenght,heading);

To compute width in meters I use this:

    public Double calcw(GoogleMap map,int ancho,LatLng p) {
    float tilt = map.getCameraPosition().tilt;
    CameraPosition old=map.getCameraPosition();
    if(tilt!=0){
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(old.target)      // Sets the center of the map to Mountain View
                .zoom(old.zoom)                   // Sets the zoom
                .bearing(old.bearing)                // Sets the orientation of the camera to east
                .tilt(0)                   // Sets the tilt of the camera to 30 degrees
                .build();
        map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    }
    Point g1 =map.getProjection().toScreenLocation(p);
    Point g2=map.getProjection().toScreenLocation(SphericalUtil.computeOffset(p,ancho/10,0));

    Double result=(distance(g1,g2));
    //Log.e("PROJ1",Double.toString(distance(g1,g2)));

    map.moveCamera(CameraUpdateFactory.newCameraPosition(old));
    return result;

        }
    public double distance(Point a, Point b)
{
    double dx = a.x - b.x;
    double dy = a.y - b.y;
    return Math.sqrt(dx * dx + dy * dy);
}
Joaquin Peraza
  • 343
  • 1
  • 15
0

Use polyline to draw line something like below,

private ArrayList<LatLng> mLatlngs ;

PolylineOptions mPolylineOptions = new PolylineOptions();
                    mPolylineOptions.color(Color.RED);
                    mPolylineOptions.width(3);
                    mPolylineOptions.addAll(mLatlngs);
                    mGoogleMap.addPolyline(mPolylineOptions);
Sreedhu Madhu
  • 2,480
  • 2
  • 30
  • 40