2

The question is pretty straightforward, but I'll give you some more context: My team is trying to make an Android application where the users can highlight a certain section of a street, color it, and send that information to a server. I've already started playing around with the Google Maps API V2, but so far the only thing I've got is a working map with zoom buttons. I've also read this thread possible to highlight a section of a street?, but there's nothing on how the user can do this from an app, instead. I was wondering, first, if this is possible at all, and second, how could it be done. Thank you.

Community
  • 1
  • 1
Hugo M. Zuleta
  • 572
  • 1
  • 13
  • 27

2 Answers2

1

I was doing something similar, but it was on Google Maps V1, over a year ago. In my case I was just drawing the lines form location points.

This is part of my old code:

class MapOverlay extends Overlay
{     
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);           

            if(startPoint != null && stopPoint != null)
            {           
                Point screenPts = new Point();

                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);             
                paint.setStrokeWidth(4);

                if(startCounter >= 0)
                {
                    int len = mapPoints.size();

                    if(route > 0)
                    {
                        route = route - (int)(5*(((((gtpPoints.get(len - route).speed)*3600)/1000))/maxSpeed)+1);               
                    }

                    if(route <= 0)
                    {
                        route = 0;
                    }

                    if(len > 1)
                    {
                        for(int i = 1; i< len - route; i++)
                        {
                            Point startPoint = new Point();
                            Point stopPoint = new Point();

                            mapView.getProjection().toPixels(mapPoints.get(i-1).point, startPoint);
                            mapView.getProjection().toPixels(mapPoints.get(i).point, stopPoint);

                            LinearGradient gradient = new LinearGradient(startPoint.x, startPoint.y, stopPoint.x, stopPoint.y, mapPoints.get(i-1).color, mapPoints.get(i).color, android.graphics.Shader.TileMode.REPEAT);
                            paint.setShader(gradient);

                            paint.setColor(mapPoints.get(i).color);
                            canvas.drawPoint(stopPoint.x, stopPoint.y, paint);

                            canvas.drawLine(startPoint.x, startPoint.y, stopPoint.x, stopPoint.y, paint);
                        }
                    }
                }

                if(startCounter > 0)
                {
                    startCounter = startCounter - 1;
                }



                if(onePoint == false)
                {
                    mapView.getProjection().toPixels(startPoint, screenPts);

                    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.start_point);
                    canvas.drawBitmap(bmp, screenPts.x-bmp.getWidth()/2, screenPts.y-bmp.getHeight()/2, null);     

                    mapView.getProjection().toPixels(stopPoint, screenPts);

                    Bitmap bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.stop_point);
                    canvas.drawBitmap(bmp2, screenPts.x-bmp2.getWidth()/2, screenPts.y-bmp2.getHeight()/2, null);     
                }
                else
                {
                    mapView.getProjection().toPixels(startPoint, screenPts);

                    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pointer);
                    canvas.drawBitmap(bmp, screenPts.x-bmp.getWidth()/2, screenPts.y-bmp.getHeight()/2, null);     
                }
            }


        return true;
    }
} 
goodm
  • 7,275
  • 6
  • 31
  • 55
1

I figured out how to do this, but it is very specific to my location, Colombia. Since Colombian street addresses are formatted "Carrera (number) #(number)-(number)" or "Calle (number) #(number)-(number)"

and the Geocoder handles addresses in Colombia like this "Carrera (number1) #(number2)-(number3) a #(number2)-(number4)" where number3 is the beginning of the street, and number4 the end of the street, what I did was a series of splits:

    private Polyline crearPolyline(List<Address> a2, GoogleMap map) {

            Address ad = a2.get(0);
            String address = ad.getAddressLine(0);
            System.out.println(address);
                //Gets "number3 a #number2-number4"
            String[] addressSplit = address.split("-");
            String addressA = null, addressB = null;
            Polyline p = null;
            try {

                //Gets number3 and "a " number4
                String[] addressSplit2 = addressSplit[1].split(" a ");

                //Start address for the polyline
                addressA=addressSplit[0]+"-"+addressSplit2[0]+", Bogotá";
                LatLng a = getLatLongFromAddress(addressA);
                            //End address for the polyline
                addressB=addressSplit[0]+"-"+addressSplit[2]+", Bogotá";
                LatLng b = getLatLongFromAddress(addressB);


                System.out.println(addressA);
                System.out.println(addressB);
                p = map.addPolyline(new PolylineOptions().add(a,b).color(Color.BLUE));

            } catch (Exception e) {

                Toast.makeText(getApplicationContext(), "Procure tocar calles rectas", Toast.LENGTH_LONG).show(); 
//If the polyline can't be painted, the street might not be straight.
            }

            return p;
        }

I hope this makes sense for anyone that hasn't been to Colombia.

Hugo M. Zuleta
  • 572
  • 1
  • 13
  • 27