在onLocationChanged上运行For循环

时间:2014-09-22 09:39:10

标签: java android

我正在尝试为单个项目构建应用程序。我有一个纬度和经度坐标数据库,以及相关的放射性水平。应用程序检查用户位置并检查它们与数据库中的点之间的距离。如果该距离小于15米,则会触发警告灯。

我能够让应用程序在数据库中读取并将其存储在类的arraylist中。我还能够让GPS每2米更新一次位置。我想在onLocationChange方法中添加一个for循环,以便应用程序检查数据库,但我不知道如何执行此操作...如何将“dataPoints”arraylist传递给locationlistener方法,以便onLocationChange可以访问它??这完成不正确吗?我在下面提供了我的代码:

public class MainActivity extends Activity{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
protected String latitude,longitude; 
protected boolean gps_enabled,network_enabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.textview1);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 2, mLocationListener);

//read in datapoints from text file in assets folder and store in class "radioactivityData" in arrayList "dataPoints"
BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(getAssets().open("combinedorderedData.txt")));
} catch (IOException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
}

//Define and initialize the ArrayList
ArrayList<radioactivityData> dataPoints = new ArrayList<radioactivityData>(); //The ArrayList stores strings

String inLine; //Buffer to store the current line
try {
    while ((inLine = reader.readLine()) != null) //Read line-by-line, until end of file
    {
        String[] parts = inLine.split("  ");
        radioactivityData rad = new radioactivityData();

        rad.setlatitude(Double.parseDouble(parts[0]));
        rad.setlongitude(Double.parseDouble(parts[1]));
        rad.setradioactivity(Integer.parseInt(parts[2]));
        dataPoints.add(rad);
    }
} catch (NumberFormatException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
try {
    reader.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} //We've finished reading the file      

}

//I think I just need to pass the dataPoints array to the LocationListener method... how? is this    wrong?
LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
    txtLat = (TextView) findViewById(R.id.textview1);
    txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());

    //Here I want to calculate the distance between current location and the data in the dataPoints array
    for(int i=0; i<dataPoints.size(); i++){
        if(getdistance(dataPoints.get(i).getlatitude(), dataPoints.get(i).getlongitude(), 
                location.getLatitude(), location.getLongitude())<15 && dataPoints.get(i).getradiation()>5000)
        {
            txtLat.setText("Turn on the green LED!");          
            break;
        }
        else
        {
            txtLat.setText("No radioactive areas nearby!");             
        }    
    }
}   

@Override
public void onProviderDisabled(String provider) {
    Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
    Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");
}


private double getDistance(double lat1, double lon1, double lat2, double lon2){

    double theta, dist;
    theta = lon1 - lon2;
    dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    dist = dist * 1.609344 * 1000;

    return (dist);
}

private double deg2rad(double deg) {
    return (deg * Math.PI / 180);
}

private double rad2deg(double rad) {
    return (rad * 180 / Math.PI);
}
};
}

如果有帮助的话,这是radioactivityData类

public class radioactivityData {
private double latitude;
private double longitude;
private int radioactivity;


public double getlatitude()
{
    return latitude;
}

public void setlatitude(double latitude) {
    this.latitude = latitude;
}

public double getlongitude()
{
    return longitude;
}

public void setlongitude(double longitude) {
    this.longitude = longitude;
}

public int getradioactivity()
{
    return radioactivity;
}

public void setradioactivity(int radioactivity) {
    this.radioactivity = radioactivity;
}
}

1 个答案:

答案 0 :(得分:0)

来自 onLocationChanged

调用您将在该方法中传递当前位置的方法获取您要与之比较的所有位置。

相关问题