0

我正在尝试创建一个 GPS 跟踪应用程序,并且一直坚持如何从前一点到当前点画一条线。我试过使用 Overlay,但它不显示...我在 Java 上不是很好,所以请像我 4 岁一样跟我说话...

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    initMapView();
    initMyLocation();

    TabHost.TabSpec spec;

    TabHost th = (TabHost)findViewById(R.id.tabhost);
    th.setup();
    spec = th.newTabSpec("tag1");
    spec.setContent(R.id.mapTab);
    spec.setIndicator("Map");
    th.addTab(spec);

    spec = th.newTabSpec("tag2");
    spec.setContent(R.id.logTab);
    spec.setIndicator("Log");
    th.addTab(spec);

    spec = th.newTabSpec("tag3");
    spec.setContent(R.id.detailsTab);
    spec.setIndicator("Details");
    th.addTab(spec);

}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

//Map and Controls
private void initMapView() {
    map = (MapView) findViewById(R.id.mvMain);
    controller = map.getController();
    map.setSatellite(true);
    //map.setStreetView(true);
    map.setBuiltInZoomControls(true);
}


//Creates an Overlay that marks current position
private void initMyLocation() {
    final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
    overlay.enableMyLocation();
    overlay.enableCompass();
    overlay.runOnFirstFix(new Runnable() {
        public void run() {
            controller.setZoom(17);
            controller.animateTo(overlay.getMyLocation());
            map.getOverlays().add(overlay);
        }

    });

}
//Experiment
public class detailsTab extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.id.detailsTab);

        LocationManager locationManager;
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(context);

        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);
        updateWithNewLocation(location);
    }

    private void updateWithNewLocation(Location location) {
        String latLongString;
        TextView myLocationText;
        myLocationText = (TextView)findViewById(R.id.detailsText);
        if(location != null){
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong" + lng;
        }
        else {
            latLongString = "No Location Found";
        }
        myLocationText.setText("Your current position is: \n" + latLongString);
    }
}

public class NewOverlay extends Overlay {
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        Projection projection = mapView.getProjection();

        Double lat = lati *1E6;
        Double lng = longi *1E6;

        GeoPoint geoPoint = new GeoPoint(lat.intValue(), lng.intValue());

        if (shadow == false) {
            Point myPoint = new Point();
            projection.toPixels(geoPoint, myPoint);

            //Creating and setting up the paint brush
            Paint paint = new Paint();
            paint.setARGB(250, 255, 0, 0);
            paint.setAntiAlias(true);
            paint.setFakeBoldText(true);

            //Create circle
            int rad = 25;
            RectF oval = new RectF(myPoint.x-rad, myPoint.y-rad, myPoint.x+rad, myPoint.y+rad);

            canvas.drawOval(oval, paint);
            canvas.drawText("Red Circle", myPoint.x+rad, myPoint.y, paint);


        }

    }
}

}

4

2 回答 2

1

我这样做,内部draw()方法,

            int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
            Paint paint = new Paint();
            paint.setColor(Color.rgb(0x7b, 0x7b, 0xff));
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(5);
            if(mPoints!=null || mPoints.size()<2)
            {
            for (int i = 0; i < mPoints.size(); i++) {
                    Point point = new Point();
                    mv.getProjection().toPixels(mPoints.get(i), point);
                    Point loc = new Point();
                    mv.getProjection().toPixels(new GeoPoint((int) (location.getLatitude()*1.0E6),(int) (location.getLongitude()*1.0E6)), loc);
                    x2 = point.x;
                    y2 = point.y;
                    if (i == 0)
                    {
                        x2 = loc.x;
                        y2 = loc.y;
                    }
                    if (i > 0) {
                        canvas.drawLine(x1, y1, x2, y2, paint);
                    }
                    x1 = x2;
                    y1 = y2;

            }
            }

这段代码将为我绘制一条完整的路线,是我想要绘制它们mPoints的数组。GeoPoint这应该对你有用。

于 2012-04-22T01:46:13.030 回答
0
public void draw(Canvas canvas, MapView mapv, boolean shadow){
    super.draw(canvas, mapv, shadow);
    Projection projection = mapv.getProjection();
    Path p = new Path();
    for (int i = 0; i < geoPointsArray.size(); i++) {
        if (i == geoPointsArray.size() - 1) {
            break;
        }
        Point from = new Point();
        Point to = new Point();
        projection.toPixels(geoPointsArray.get(i), from);
        projection.toPixels(geoPointsArray.get(i + 1), to);
        p.moveTo(from.x, from.y);
        p.lineTo(to.x, to.y);
    }
    Paint mPaint = new Paint();
    mPaint.setStyle(Style.STROKE);
    mPaint.setColor(Color.GREEN);
    mPaint.setAntiAlias(true);
    mPaint.setStrokeWidth(5);
    canvas.drawPath(p, mPaint);
    mapv.invalidate();
    super.draw(canvas, mapv, shadow);
}//draw()

我们可以使用上述方法绘制路线,因为我们移动一个地方到另一个地方。其中 geoPointsArray 是接收到的位置数组。在您的 onCreate() 中,您需要调用 mapv.invalidate(); 不断地画路线

于 2012-04-22T02:19:43.890 回答