开发者

Drawing a line between 2 geopoints using Android 2.3

i am trying to draw a line between 2 geopoints. i am able to show to geopoints on the map. Its working fine. but i am not able to draw a line between 2 points. program has no error but line is not getting displayed. can anyone tell me what i have to change.

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---
        Bit开发者_如何学Cmap 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;
}          

}


Forget all the Path stuff, just use the following line (it works for me):

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


I suspect you need to change this line:

    mPaint.setStyle(Style.FILL);

to:

    mPaint.setStyle(Style.STROKE);

Also, while it's probably not related to your problem, it looks like you are calling super.draw twice, once at the beginning and once at the end. You probably just want to call it at the beginning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜