2012-02-28 34 views
0

我想知道是否可以使用用戶輸入來繪製地圖疊加線?如在用戶中選擇兩個點並在它們之間畫一條線。我有一些代碼從屏幕的左上角繪製一條線到MapView上的一個點。當我點擊一個點時,線條移動到它。我如何設置這個只畫兩條被點擊的點之間的線條?使用用戶輸入在Google Map API for Android中使用Overlay繪製線條?

我很新的這東西,所以在此先感謝您的幫助

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

}

回答

1

你可以試試this。它使用位置,然後轉換爲GeoPoint。您可以在用戶觸摸屏幕時獲取位置信息

相關問題