java两次获得相同的纬度、经度值,比较并保存到db
我从GPS获取纬度和经度值,如果我两次获得相同的位置值(纬度和经度),那么我需要比较阵列列表值,将这些值添加到db,并从阵列列表中删除多个值。 在我的代码中,在逻辑不起作用之后,第一次将值添加到db中。谁能帮帮我吗。提前谢谢
我的代码
主要活动。java
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private TextView latitudeVal, longitudeVal;
private Sensor accelerometer;
private SensorManager sm;
private LocationManager locationManager;
private LocationListener locationListener;
private DBHandler dbHandler;
private Location current;
private List longitudeList;
private List latitudeList;
private int countLongitude = 0;
private int countLatitude = 0;
private double longValue;
private double latValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHandler = new DBHandler(this, null, null, 1);
current = new Location("Dummy");
longitudeList = new ArrayList<>();
latitudeList = new ArrayList<>();
latitudeVal = (TextView) findViewById(R.id.latValue);
longitudeVal = (TextView) findViewById(R.id.longValue);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI); //registered sensor manager
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
latitudeVal.setText("= " + location.getLatitude()); // getting latitude value
longitudeVal.setText("= " + location.getLongitude()); // getting longitude value
current = location;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
//runtime permissions for gps
if (ActivityCompat.checkSelfPermission(this, 安卓.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 安卓.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{
安卓.Manifest.permission.INTERNET, 安卓.Manifest.permission.ACCESS_COARSE_LOCATION, 安卓.Manifest.permission.ACCESS_FINE_LOCATION
}, 10);
}
return;
} else {
locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
try {
switch (requestCode) {
case 10:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
}
}catch (SecurityException e){
e.printStackTrace();
}
}
@Override
public void onSensorChanged(SensorEvent event) {
long actualTime = event.timestamp;
try{
if(actualTime - lastUpdate > 100000000) {
lastUpdate = actualTime;
//logic for adding if we get same
double longi = Math.round(current.getLongitude() * 1000.0) / 1000.0; // rounding off the values
double lati = Math.round(current.getLatitude() * 1000.0) / 1000.0;
longitudeList.add(longi);
latitudeList.add(lati);
if(longitudeList.size() > 1 && latitudeList.size() > 1){
for (int i = 0; i < longitudeList.size(); i++) {
for (int j = i+1; j < longitudeList.size(); j++) {
// compare list.get(i) and list.get(j)
countLongitude++;
}
}
for (int i = 0; i < longitudeList.size(); i++) {
for (int j = i+1; j < longitudeList.size(); j++) {
// compare list.get(i) and list.get(j)
countLatitude++;
}
}
if(countLatitude == 2 && countLongitude == 2) {
//adding data to db
longValue = current.getLongitude();
latValue = current.getLatitude();
dbHandler.addLocation(current);
countLongitude = 0;
countLatitude = 0;
for(int i=0; i<longitudeList.size(); i++){
if(String.valueOf(longValue).equals(longitudeList.get(i)))
longitudeList.remove(i);
}
}
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
主要活动。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:layout_marginLeft="10dp"
安卓:orientation="vertical"
tools:context="com.demo.gps">
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="10dp"
安卓:orientation="horizontal">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Latitude"
安卓:textColor="@安卓:color/holo_green_light"
安卓:textSize="20sp" />
<TextView
安卓:id="@+id/latValue"
安卓:layout_width="wrap_content"
安卓:text=""
安卓:layout_height="wrap_content"
安卓:textColor="@安卓:color/holo_green_light"
安卓:layout_marginLeft="30dp"
安卓:textSize="20sp" />
</LinearLayout>
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="10dp"
安卓:orientation="horizontal">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textColor="@安卓:color/holo_green_light"
安卓:text="Longitude"
安卓:textSize="20sp" />
<TextView
安卓:id="@+id/longValue"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginLeft="15dp"
安卓:textColor="@安卓:color/holo_green_light"
安卓:text=""
安卓:textSize="20sp" />
</LinearLayout>
</LinearLayout>
AndroidManifest。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.demo.gps">
<uses-permission 安卓:name="安卓.permission.INTERNET" />
<uses-permission 安卓:name="安卓.permission.ACCESS_COARSE_LOCATION" />
<uses-permission 安卓:name="安卓.permission.ACCESS_FINE_LOCATION" />
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/app"
安卓:label="@string/app_name"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<activity 安卓:name=".MainActivity">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
# 1 楼答案
在onSensorChanged()方法中,仅当列表中不包含纬度和经度值时,才将其添加到列表中。要检查值是否存在,请使用列表。contains()方法
试试这个代码