0

GPS 追跡アプリを作成しようとしていますが、前のポイントから現在のポイントまで線を引く方法に固執しています。オーバーレイを使用してみましたが、表示されません... 私は 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 に答える