2

这个问题很简单,但我会给你一些更多的背景信息:我的团队正在尝试制作一个 Android 应用程序,用户可以在其中突出显示街道的某个部分,为其着色,并将该信息发送到服务器。我已经开始使用 Google Maps API V2,但到目前为止,我所拥有的只是一张带有缩放按钮的工作地图。我也读过这个帖子可以突出街道的一部分?,但没有关于用户如何从应用程序执行此操作的信息。我想知道,首先,这是否可能,其次,如何做到这一点。谢谢你。

4

2 回答 2

1

我正在做类似的事情,但它是在一年前的 Google Maps V1 上。就我而言,我只是在位置点上画线。

这是我的旧代码的一部分:

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;
    }
} 
于 2013-09-26T16:13:55.047 回答
1

我想出了如何做到这一点,但它非常适合我所在的哥伦比亚。由于哥伦比亚街道地址的格式为“Carrera (number) #(number)-(number)”或“Calle (number) #(number)-(number)”

并且地理编码器处理哥伦比亚的地址,如下所示“Carrera (number1) #(number2)-(number3) a #(number2)-(number4)”,其中 number3 是街道的起点,而 number4 是街道的尽头,什么我做了一系列的分裂:

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

我希望这对没有去过哥伦比亚的人有意义。

于 2013-10-25T03:05:45.903 回答