简体   繁体   English

使用Android 2.3在2个地理位置之间绘制一条线

[英]Drawing a line between 2 geopoints using Android 2.3

i am trying to draw a line between 2 geopoints. 我试图在2个地理点之间画一条线。 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. 但我无法在2点之间划清界限。 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---
        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;
}          

}

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

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. 另外,虽然它可能与您的问题无关,但似乎您在两次调用super.draw,一次在开始,一次在结束。 You probably just want to call it at the beginning. 您可能只想在开始时就调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM