使用Android 2.3在2个地理点之间画一条线

时间:2011-01-31 11:10:09

标签: android

我想在2个地理点之间画一条线。我能够在地图上显示地理位置。 它的工作正常。但我无法在两点之间划一条线。程序没有错误,但没有显示行。任何人都可以告诉我我必须改变什么。

public class HelloMapView extends MapActivity {
/** Called when the activity is first created. */
 LinearLayout linearLayout;
MapView mapView; 
MapController mc;
GeoPoint p,p1;

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

        //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

        //---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(
            getResources(), R.drawable.a);             
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);  

        //Coordinates 2 

      //---translate the GeoPoint to screen pixels---
        Point screenPts1 = new Point();
        mapView.getProjection().toPixels(p1, screenPts1);

        //---add the marker---
        Bitmap bmp1 = BitmapFactory.decodeResource(
            getResources(), R.drawable.b);             
        canvas.drawBitmap(bmp1, screenPts1.x, screenPts1.y-50, null); 

        //----------- Start--------------//

        Projection projection = mapView.getProjection();
        Path path = new Path();

        Point from = new Point();
        Point to = new Point();
        projection.toPixels(p, from);
        projection.toPixels(p1, to);
        path.moveTo(from.x, from.y);
        path.lineTo(to.x, to.y);

        Paint mPaint = new Paint();
        mPaint.setStyle(Style.FILL);
        mPaint.setColor(0xFFFF0000);
        mPaint.setAntiAlias(true);
        canvas.drawPath(path,mPaint);
        super.draw(canvas, mapView, shadow);         


        return true;
    }
} 


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

    mapView.setBuiltInZoomControls(true);

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



    mapView.displayZoomControls(true);
    mc = mapView.getController();
    String coordinates[] = {"12.958998", "77.658998"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

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


    String coordinates1[] = {"12.95967","77.64918"};
    double lat1 = Double.parseDouble(coordinates1[0]);
    double lng1 = Double.parseDouble(coordinates1[1]);

    p1 = new GeoPoint(
        (int) (lat1 * 1E6), 
        (int) (lng1 * 1E6));


    mc.animateTo(p);
    mc.animateTo(p1);
    mc.setZoom(16); 

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

    mapView.invalidate();


}

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

}

2 个答案:

答案 0 :(得分:0)

忘记所有Path的东西,只需使用以下行(它适用于我):

canvas.drawLine(screenPts.x, screenPts.y, screenPts1.x, screenPts1.y, mPaint);

答案 1 :(得分:0)

我怀疑你需要改变这一行:

    mPaint.setStyle(Style.FILL);

为:

    mPaint.setStyle(Style.STROKE);

此外,虽然它可能与您的问题无关,但看起来您正在调用super.draw两次,一次是在开头,一次是在结尾。您可能只想在开头调用它。