在带有用户输入的 Google Map API for Android 中使用 Overlay 绘制一条线?

Drawing a line using Overlay in Google Map API for Android with user inputs?

我想知道是否可以使用用户输入通过地图叠加绘制一条线?就像用户选择两个点并在它们之间画一条线一样。我有一些代码从屏幕的左上角画一条线到 MapView 上的一个点。当我单击一个点时,线会移动到它。我如何将其设置为仅在已单击的两个点之间画线?

我对这些东西很陌生,所以提前感谢您的帮助

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
public class LBServicesActivity extends MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;
private ArrayList<GeoPoint> pointsList = new ArrayList();
float x, y;
int touchCount = 0;
String tag ="";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.mapView);

    mc = mapView.getController();

    String coordinates[] = {"1.352566007","103.78921587"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
        (int) (lat * 1E6),
        (int) (lng * 1E6));

    mc.setZoom(3);

    //-- Add location marker
    MapOverlay mapOverlay = new MapOverlay();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);

    //mapView.invalidate();    
}


//Zoom into certain area
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    MapController mc = mapView.getController();

    for(int i = 0; i< 2; i++)
    {
        GeoPoint ge = new GeoPoint(0,0);
        ge = pointsList.get(i);

        System.out.println(ge.getLatitudeE6());
        System.out.println(ge.getLongitudeE6());
    }

    switch(keyCode)
    {
        case KeyEvent.KEYCODE_3:
            mc.zoomIn();
            break;
        case KeyEvent.KEYCODE_1:
            mc.zoomOut();
            break;
    }
    return super.onKeyDown(keyCode, event);

}

@Override
protected boolean isRouteDisplayed()
{
    return false;
}

public  class MapOverlay extends com.google.android.maps.Overlay
{      
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow)
    {      
        //super.draw(canvas,mapView,shadow);

        //-- Create new paint object --
        Paint mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(2);

        //--GeoPoint--
        //Start Point of the Rectangle
        //GeoPoint sP = new GeoPoint()

        //--Translate  the GeoPoint to screen pixels --
        Point screenPts = new Point();
        Point startPoint = new Point();

        mapView.getProjection().toPixels(p,  screenPts);
        //mapView.getProjection().toPixels(arg0, startPoint)


        //-- Draw a line between two points --
        canvas.drawLine(0 , 0, screenPts.x, screenPts.y, mPaint);

        //return true;
    }

    public boolean onTouchEvent(MotionEvent event,MapView mapView)
    {
        //-- When User lifts his finger --
        if(event.getAction() ==1)
        {
            p = mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());

            //Add p to the array called pointsList
            pointsList.add(p);

            //Counts the number of times the screen has been touched
            for(int i =0; i< pointsList.size();i++)
            {
                GeoPoint points = new GeoPoint(pointsList.get(i).getLatitudeE6(),pointsList.get(i).getLongitudeE6());
                System.out.print(pointsList.get(i));
                System.out.print(points.toString());
            }

            //Display the lat and long of the point touched
            Toast.makeText(getBaseContext(),"Location:" + p.getLatitudeE6() / 1E6 +","
                                                         + p.getLongitudeE6() / 1E6, Toast.LENGTH_SHORT).show();
            touchCount = touchCount+1;
            Log.d(tag,"This is the onTouchEvent No#:" + touchCount);
        }

        return false;
    }
}

public void onStop()
{
    super.onStop();
    Log.d(tag,"In the onStop() event");
}

public void onDestroy()
{
    super.onDestroy();
    Log.d(tag,"In the onDestroy() event");
}

}


你可以试试这个。它使用位置,然后转换为 GeoPoint。您可以在用户触摸屏幕时从用户那里获取位置