1

我正在尝试使用 aChartEngine 将 GPS 数据(视图中或使用中的卫星)显示为条形图,但我没有在此视图中显示任何数据。这是我的代码,你能说出我犯了什么错误吗?

public class SmartHikingActivity extends Activity implements LocationListener{
LocationManager Lm;
LocationProvider Lp;
static List<String> PL;
GpsSatellite Sat;
static Iterable<GpsSatellite> Sats;
static ArrayList<SatelliteData> LSats;
TextView txtTime;
TextView txtDate;
TextView txtLat;
TextView txtLon;
TextView txtAccuracy;
TextView txtAltitude;
TextView txtSpeed;
TextView txtBearing;
TextView txtGpsPower;
TextView txtInView;
TextView txtInUse;
Calculations C;


LinearLayout layout;

static XYMultipleSeriesDataset dataset;
static XYMultipleSeriesRenderer renderer;
static XYSeriesRenderer rendererSeries;
static CategorySeries series;
static GraphicalView mChartView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lyt_gps);

    layout = (LinearLayout) findViewById(R.id.chart);
    txtTime=(TextView)findViewById(R.id.txtTime);
    txtDate=(TextView)findViewById(R.id.txtDate);
    txtLat=(TextView)findViewById(R.id.txtLat);
    txtLon=(TextView)findViewById(R.id.txtLon);
    txtAccuracy=(TextView)findViewById(R.id.txtAccuracy);
    txtAltitude=(TextView)findViewById(R.id.txtAltitude);
    txtSpeed=(TextView)findViewById(R.id.txtSpeed);
    txtBearing=(TextView)findViewById(R.id.txtBearing);
    txtGpsPower=(TextView)findViewById(R.id.txtGpsPower);
    txtInUse=(TextView)findViewById(R.id.txtInUse);
    txtInView=(TextView)findViewById(R.id.txtInView);
    C=new Calculations();

    Lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
    PL=Lm.getAllProviders();
    LSats = new ArrayList<SatelliteData>();

    Lm.addGpsStatusListener(gpsListener);
    if(Lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        Lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 30, 500.0f, this);
    }

    dataset = new XYMultipleSeriesDataset();
    renderer = new XYMultipleSeriesRenderer();

    renderer.setChartTitle("Satellites Status");
    renderer.setXTitle("SATS");
    renderer.setYTitle("SNR");
    renderer.setXAxisMin(0);
    renderer.setXAxisMax(24);
    renderer.setYAxisMin(0);
    renderer.setYAxisMax(120);
    renderer.setClickEnabled(false);
    renderer.setExternalZoomEnabled(false);
    renderer.setPanEnabled(false, false);
    renderer.setZoomEnabled(false, false);
    renderer.setBarSpacing(0.5);
    renderer.setAxisTitleTextSize(16);
    renderer.setChartTitleTextSize(20);
    renderer.setLabelsTextSize(15);
    renderer.setLegendTextSize(15);
    renderer.setMargins(new int[] {20, 30, 15, 0});
    rendererSeries = new XYSeriesRenderer();
    rendererSeries.setColor(Color.GRAY);
    rendererSeries.setDisplayChartValues(true);
    renderer.addSeriesRenderer(rendererSeries);

    //renderer = getBarDemoRenderer();
    //Intent intent = ChartFactory.getBarChartIntent(this, getBarDemoDataset(), renderer, Type.DEFAULT);
    //startActivity(intent);
    //mChartView = ChartFactory.getBarChartView(this, dataset, renderer, Type.DEFAULT);
    //layout.addView(mChartView);

}

@Override
public void onPause(){
    super.onPause();
    Lm.removeUpdates(this);
}

@Override
public void onResume(){
    super.onResume();
    Lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 30, 500.0f, this);
}


public void onLocationChanged(Location loc) {
    txtLat.setText(C.convertToDMS(loc.getLatitude(),"Lat"));
    txtLon.setText(C.convertToDMS(loc.getLongitude(),"Lon"));
    txtAccuracy.setText(String.valueOf(loc.getAccuracy()));
    txtAltitude.setText(String.valueOf(loc.getAltitude()));
    txtSpeed.setText(String.valueOf(loc.getSpeed()));
    txtBearing.setText(String.valueOf(loc.getBearing()));
}

public void onProviderDisabled(String provider) {

}

public void onProviderEnabled(String provider) {

}

public void onStatusChanged(String provider, int status, Bundle extras) {

}

private  GpsStatus.Listener gpsListener = new GpsStatus.Listener() {
    public void onGpsStatusChanged(int event) {
        GpsStatus gpsStatus = Lm.getGpsStatus(null);
        switch (event) {
            case GpsStatus.GPS_EVENT_STARTED: 
                //tvDemo.setText( "onGpsStatusChanged(): GPS started");
                break;
            case GpsStatus.GPS_EVENT_FIRST_FIX: 
                //tvDemo.setText("onGpsStatusChanged(): time to first fix in ms = " + gpsStatus.getTimeToFirstFix());
                break;
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS: 
                Iterable<GpsSatellite>satellites = gpsStatus.getSatellites();
                Iterator<GpsSatellite>satI = satellites.iterator();
                while (satI.hasNext()) {
                    GpsSatellite sat = satI.next();
                    SatelliteData SD = new SatelliteData();
                    SD.setPRN(sat.getPrn());
                    SD.setSNR(sat.getSnr());
                    SD.setUIF(sat.usedInFix());
                    if(!SatExist(LSats,sat.getPrn())){
                        LSats.add(SD);
                    }else{
                        UpdateSat(LSats,sat.getPrn(),SD);
                    }
                }
                String line="";
                int Total=0;
                int inUse=0;
                try{
                for(int i=0;i<LSats.size();i++){
                    line= line + "PRN: " + LSats.get(i).getPRN() + " SNR: " + LSats.get(i).getSNR() + " UIF: "  + LSats.get(i).isUIF() + "\n";
                    Total=Total+1;
                    if(LSats.get(i).isUIF()) inUse=inUse+1;
                }
                }catch(NullPointerException Npe){

                }
                txtInView.setText(String.valueOf(Total));
                txtInUse.setText(String.valueOf(inUse));
                Log.v("", "Triggering refresh chart");/////////////////////////
                series = new CategorySeries("Satellites");
                try{
                  Log.v("", "Filling Dataset");
                  for (int k = 0; k < LSats.size(); k++) {
                    series.add(LSats.get(k).getSNR());
                  }
                }catch(Exception e){
                  Log.v("", "Error Filling Dataset");
                }
                ///////////////////////////////////////////
                dataset.addSeries(series.toXYSeries());
                if (mChartView==null){
                    mChartView = ChartFactory.getBarChartView(SmartHikingActivity.this, dataset, renderer, Type.DEFAULT);
                    layout.addView(mChartView);
                    mChartView.refreshDrawableState();
                    mChartView.repaint();
                }
                break;
            case GpsStatus.GPS_EVENT_STOPPED: 
                break;
        }       
    }
};

private boolean SatExist(List<SatelliteData> lSD, int PRN){
    boolean Exist=false;
    try{
        for(int i=0; i < lSD.size();i++){
            if (lSD.get(i).getPRN()==PRN){
                Exist= true;
                break;
            }
        }
    }catch(NullPointerException Npe){
        Exist=false;
    }
    return Exist;
}

private void UpdateSat(List<SatelliteData> lSD, int PRN, SatelliteData sD){
    for(int i=0; i < lSD.size();i++){
        if (lSD.get(i).getPRN()==PRN){
            lSD.get(i).setSNR(sD.getSNR());
            lSD.get(i).setUIF(sD.isUIF());
            break;
        }
    }
}

}

图表必须在 onGpsStatusChanged 事件中更新。

非常感谢

4

1 回答 1

1

这段代码似乎不对:

if (mChartView==null){
  mChartView = ChartFactory.getBarChartView(SmartHikingActivity.this, dataset, renderer, Type.DEFAULT);
  layout.addView(mChartView);
  mChartView.refreshDrawableState();
  mChartView.repaint();
}

它应该是这样的:

if (mChartView==null){
  mChartView = ChartFactory.getBarChartView(SmartHikingActivity.this, dataset, renderer, Type.DEFAULT);
  layout.addView(mChartView);
}
mChartView.refreshDrawableState();
mChartView.repaint();

在这里查看一个有效的示例。

于 2012-07-09T07:11:32.513 回答