5

How to draw line on MapView given coordinates?

AFAIK, on iPhone it is possible.

Please advise.

Thanks in advance.

Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
AndroiDBeginner
  • 3,571
  • 11
  • 40
  • 43
  • 1
    There is a more complete answer to your question here: http://stackoverflow.com/questions/2176397/drawing-a-line-path-on-google-maps – gfrigon May 31 '12 at 22:42

2 Answers2

21

To use a MapView your Activity must extend MapActivity.

For each line you want to draw (or really anything else) you need to subclass Overlay and do the drawing in the Overlay's onDraw() method. Once you've created your Overlay add it to the MapView with something like mMapView.getOverlays().add(new MyOverlay());.

Inside your custom Overlay you'll want to get a Projection with something like Projection p = mapView.getProjection();. From the Projection you can convert GPS coordinates into screen coordinates with Projection's toPixels(GeoPoint, Point) method and then just draw to the passed in Canvas using normal Android 2D drawing methods.

That's the basics... if you need anything else, just ask.

Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
0

You can use this code, sample coordinates and its usages.

//...setting map and starting 

ArrayList<LatLng> list = new ArrayList<>();
list.add(new LatLng(41.020244, 29.045663));
list.add(new LatLng(41.019904, 29.045448));
list.add(new LatLng(41.019451, 29.044397));
list.add(new LatLng(41.019710, 29.043474));

PolylineOptions options = new PolylineOptions()
    .width(5)
    .color(Color.BLUE)
    .geodesic(true);

for (int z = 0; z < list.size(); z++) {
    LatLng point = list.get(z);
    options.add(point);
}

CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(new LatLng(41.020811, 29.046113))
    .zoom(15)
    .build();

mGoogleMap.addPolyline(options);
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
SerjantArbuz
  • 982
  • 1
  • 12
  • 16
Arda Kaplan
  • 1,720
  • 1
  • 15
  • 23