Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行For循环onLocationChanged_Java_Android - Fatal编程技术网

Java 运行For循环onLocationChanged

Java 运行For循环onLocationChanged,java,android,Java,Android,我正在尝试为单个项目构建一个应用程序。我有一个关于纬度和经度坐标以及相关放射性水平的数据库。应用程序会检查用户的位置,并检查他们与数据库中的点之间的距离。如果这个距离小于15米,它将触发一个警告灯 我能够让应用程序读取数据库并将其存储在类的arraylist中。我还能够让GPS每2米更新一次位置。我想在onLocationChange方法中添加一个for循环,以便应用程序检查数据库,但我不确定如何执行此操作。。。如何将“dataPoints”arraylist传递给locationlistene

我正在尝试为单个项目构建一个应用程序。我有一个关于纬度和经度坐标以及相关放射性水平的数据库。应用程序会检查用户的位置,并检查他们与数据库中的点之间的距离。如果这个距离小于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);
}
};
}

onLocationChanged调用一个方法,将当前位置传递到该方法中,该方法本身将获取所有要与之比较的位置。

这是有效的。我还将arrayList放在onCreate方法之外的文件顶部。
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;
}
}