2014-09-22 108 views
0

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

我能够让应用程序读取数据库并将其存储在类的数组列表中。我还能够让GPS每2米更新一次位置。我想在onLocationChange方法中添加一个for循环,以便应用程序检查数据库,但我不知道如何执行此操作......我如何将“dataPoints”数组列表传递给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; 
} 
} 

回答

0

onLocationChanged调用一个方法,你将在该方法本身传递当前的位置获取您想要比较的所有地点。

+0

谢谢,我会试试这个并更新你! – Pixelsoldier 2014-09-22 09:48:05

+0

这工作。我还将arrayList放在onCreate方法之外的文件顶部。 – Pixelsoldier 2014-09-22 10:12:53