well, im a begginer in android and i need to use maps on the device more specifically (polylines) i need to do something like this.
this is a web app i did to track down bus routes and bus-stops on my city , and i've been asked to do the same thing in android! ive been checking the maps api for android and did not found anything similar to polyline in JS api , is there a way to achieve this?
i have no problem adding simple overlays i've been checking the basic tutorials in android developer site, but i dont know how to draw the polyline.
There no such API in Android Google Maps API. You can only first list the actual GeoPoints of the route that you want to draw and then draw the points and lines on a Overlay object. There's just no easy way to do that.
A more easy way to do that is get your points and extend the ImageView that will display your image to draw the points, than you just need to pass the points that you want to draw .
In my project I did this:
public class ImageDraw extends ImageView{
private Paint mPaint = new Paint();
List<Point> pts = new ArrayList<Point>() ;
public ImageDraw(Context context) {
super(context);
}
//used to send the location of the points to draw on the screen
//must be called before every redraw to update the points on the screen
public void SetPointsToDraw(List<Point> pts)
{
this.pts = pts;
}
public ImageDraw(Context context, AttributeSet attrs)
{
super(context,attrs);
}
public ImageDraw(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paintColor = mPaint;
paintColor.setColor(Color.YELLOW);
paintColor.setStrokeWidth(3);
if(pts.size() > 0)
{
canvas.drawCircle(pts.get(0).x, pts.get(0).y, 7, paintColor);
}
if (pts.size() > 1)
{
for (int i = 1 ; i < pts.size(); i++) {
paintColor.setColor(Color.YELLOW);
canvas.drawCircle(pts.get(i).x, pts.get(i).y, 7, paintColor);
paintColor.setColor(Color.RED);
canvas.drawLine(pts.get(i-1).x, pts.get(i-1).y, pts.get(i).x, pts.get(i).y, paintColor);
}
}
}
}
When you extends the Imageview and create the layout with xml don`t forget to put the entire package of you new widget like:
com.Myapp.MyImageView
FvZ's answer works but is not the native way, there are polylines on the map, a simple example
https://stackoverflow.com/a/21877742/618419
Also take a look at the Android Documentation, they have many simple and well put-together examples:
http://developer.android.com/reference/com/google/android/gms/maps/model/Polyline.html