我有问题;我的je幸是我有2个数据库,首先在sqlite db中使用x,y(Lat,Long)数据进行收藏夹定位。其次是战役数据库相同的x,y坐标。有很多不同的数据,例如45.12345和35.12345,我想从数据库中读取数据并计算它们之间的距离,我正在使用光标移动下一个数据,但无法到达Campaigns db中的常用位置,或者无法读取相同方法或字段中的数据来计算距离?
第一个数据库的代码1:
public List<DataModel> listFavoriteLocation(){
db = new DatabaseHelper(this);
SQLiteDatabase mydb = db.getWritableDatabase();
List<DataModel> data=new ArrayList<>();
Cursor csr = mydb.rawQuery("select * from "+TABLE+" ;",null);
StringBuffer stringBuffer = new StringBuffer();
DataModel dataModel = null;
while (csr.moveToNext()) {
dataModel= new DataModel();
String FAVCurrentLocationLAT = csr.getString(csr.getColumnIndexOrThrow("FAVCurrentLocationLAT"));
String FAVCurrentLocationLONG = csr.getString(csr.getColumnIndexOrThrow("FAVCurrentLocationLONG"));
dataModel.setFAVCurrentLocationLAT(FAVCurrentLocationLAT);
dataModel.setFAVCurrentLocationLONG(FAVCurrentLocationLONG);
stringBuffer.append(dataModel);
data.add(dataModel);
}
for (DataModel mo:data ) {
this.List_FAVCurrentLocationLAT = mo.getFAVCurrentLocationLAT();
this.List_FAVCurrentLocationLONG = mo.getFAVCurrentLocationLONG();
Log.i("helloLAT",""+List_FAVCurrentLocationLAT); //OK
Log.i("helloLONG",""+List_FAVCurrentLocationLONG); //OK
}
return data;
}
第二个数据库的代码2:
public List<DataModelCampaigns> Campaigns(){
db2 = new DatabaseHelperCampaigns(this);
SQLiteDatabase mydb2 = db2.getWritableDatabase();
List<DataModelCampaigns> data=new ArrayList<>();
Cursor csr2 = mydb2.rawQuery("select * from "+TABLE2+" ;",null);
StringBuffer stringBuffer = new StringBuffer();
DataModelCampaigns dataModel = null;
while (csr2.moveToNext()) {
dataModel= new DataModelCampaigns();
String CampaignsLAT = csr2.getString(csr2.getColumnIndexOrThrow("CampaignsLAT"));
String CampaignsLONG = csr2.getString(csr2.getColumnIndexOrThrow("CampaignsLONG"));
dataModel.setCampaignsLAT(CampaignsLAT);
dataModel.setCampaignsLONG(CampaignsLONG);
stringBuffer.append(dataModel);
data.add(dataModel);
}
for (DataModelCampaigns mo:data ) {
this.CampaignsLAT = mo.getCampaignsLONG();
this.CampaignsLONG = mo.getCampaignsLAT();
Log.i("helloCampaignsLAT",""+CampaignsLAT); //OK
Log.i("helloCampaignsLONG",""+CampaignsLONG); //OK
}
return data;
}
计算代码不是答案所必需的,但是通过Google搜索到达某人可以受益
double theta = foo2 - y;
double dist = Math.sin(Math.toRadians(foo)) * Math.sin(Math.toRadians(x)) + Math.cos(Math.toRadians(foo)) * Math.cos(Math.toRadians(x)) * Math.cos(Math.toRadians(theta));
dist = Math.acos(dist); // radian
dist = Math.toDegrees(dist); //degree
dist = dist * 60 * 1.1515; //to km
dist = dist * 1.609344; // to km...
if (dist <= 50)
{
//Log.i("NEAR",""+dist);
}
答案 0 :(得分:3)
根据您的代码,一种方法可能是不尝试使用以字符串形式存储的最后一个值:-
FAVCurrentLocationLAT, FAVCurrentLocationLONG, CampaignsLAT and CampaignsLONG
按照(对于最后一对):-
String CampaignsLAT = csr2.getString(csr2.getColumnIndexOrThrow("CampaignsLAT"));
String CampaignsLONG = csr2.getString(csr2.getColumnIndexOrThrow("CampaignsLONG"));
但是要改为使用每种方法返回的对象列表。
例如作为一起处理列表的示例(不是在方法中,而是可以很容易地放入方法中,只是我认为不将其作为演示方法会更简洁):-< / p>
addTestData();
mydatamodellist = listFavoriteLocation(); // declared using List<DataModel> mydatamodellist;
mydatamodelcampaignslist = Campaigns(); // declared using List<DataModelCampaigns> mydatamodelcampaignslist;
for (DataModel dm: mydatamodellist) {
for (DataModelCampaigns dmc: mydatamodelcampaignslist) {
Log.i("VALUES",
"Favourite is " + dm.getFAVCurrentLocationLAT() + "(lat) by " + dm.getFAVCurrentLocationLONG() + "(long)" +
"\tCampaign is " + dmc.getCampaignsLAT() + "(lat) by " + dmc.getCampaignsLONG() + "(long)"
);
}
}
addTestData 根据以下内容添加数据:-
db.add("1.1","10.10");
db.add("6.6","6.6");
db.add("7.7","7.7");
db.add(new DataModel("8.8","8.8"));
db2.add("1.1","1.1");
db2.add("2.2","2.2");
DataModelCampaigns dmc = new DataModelCampaigns("3.3","3.3");
db2.add(dmc);
db2.add("4.4","4.4");
db2.add(new DataModelCampaigns("1.1","10.1"));
日志包括:-
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLAT: 1.1
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLONG: 10.10
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLAT: 6.6
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLONG: 6.6
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLAT: 7.7
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLONG: 7.7
2019-04-30 09:48:42.795 7610-7610/q.campaigns I/helloLAT: 8.8
2019-04-30 09:48:42.796 7610-7610/q.campaigns I/helloLONG: 8.8
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLAT: 1.1
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLONG: 1.1
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLAT: 2.2
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLONG: 2.2
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLAT: 3.3
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLONG: 3.3
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLAT: 4.4
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLONG: 4.4
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLAT: 10.1
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/helloCampaignsLONG: 1.1
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long) Campaign is 1.1(lat) by 1.1(long)
2019-04-30 09:48:42.800 7610-7610/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long) Campaign is 2.2(lat) by 2.2(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long) Campaign is 3.3(lat) by 3.3(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long) Campaign is 4.4(lat) by 4.4(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long) Campaign is 1.1(lat) by 10.1(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long) Campaign is 1.1(lat) by 1.1(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long) Campaign is 2.2(lat) by 2.2(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long) Campaign is 3.3(lat) by 3.3(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long) Campaign is 4.4(lat) by 4.4(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long) Campaign is 1.1(lat) by 10.1(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long) Campaign is 1.1(lat) by 1.1(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long) Campaign is 2.2(lat) by 2.2(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long) Campaign is 3.3(lat) by 3.3(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long) Campaign is 4.4(lat) by 4.4(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long) Campaign is 1.1(lat) by 10.1(long)
2019-04-30 09:48:42.801 7610-7610/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long) Campaign is 1.1(lat) by 1.1(long)
2019-04-30 09:48:42.802 7610-7610/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long) Campaign is 2.2(lat) by 2.2(long)
2019-04-30 09:48:42.802 7610-7610/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long) Campaign is 3.3(lat) by 3.3(long)
2019-04-30 09:48:42.802 7610-7610/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long) Campaign is 4.4(lat) by 4.4(long)
2019-04-30 09:48:42.803 7610-7610/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long) Campaign is 1.1(lat) by 10.1(long)
通过添加类:-
public class FavoutiteCampaignLocationDifference {
private DataModel Fav;
private DataModelCampaigns Campaign;
FavoutiteCampaignLocationDifference(DataModel fav, DataModelCampaigns campaign) {
this.Fav = fav;
this.Campaign = campaign;
}
public DataModel getFav() {
return Fav;
}
public void setFav(DataModel fav) {
Fav = fav;
}
public DataModelCampaigns getCampaign() {
return Campaign;
}
public void setCampaign(DataModelCampaigns campaign) {
Campaign = campaign;
}
public String getDifference() {
//<<<<<<<<<< assumes the same elevation/height >>>>>>>>>>
double earth_radius = 6371;
double fav_lattitude = Double.valueOf(Fav.getFAVCurrentLocationLAT());
double fav_longitude = Double.valueOf(Fav.getFAVCurrentLocationLONG());
double cam_lattitude = Double.valueOf(Campaign.getCampaignsLAT());
double cam_longitude = new Double(Campaign.getCampaignsLONG());
double lat_distance = Math.toRadians(fav_lattitude - cam_lattitude);
double long_distance = Math.toRadians(fav_longitude - cam_longitude);
double arc = (Math.sin(lat_distance / 2)
* Math.sin(lat_distance /2 ))
+ Math.cos(Math.toRadians(fav_lattitude)) * Math.cos(Math.toRadians(cam_lattitude))
* Math.sin(long_distance / 2) * Math.sin(long_distance / 2);
double c = 2 * Math.atan2(Math.sqrt(arc), Math.sqrt(1 - arc));
double distance = Math.pow(earth_radius * c * 1000,2);
return String.valueOf(distance);
}
}
,然后使用:-
mydatamodellist = listFavoriteLocation(); // declared using List<DataModel> mydatamodellist;
mydatamodelcampaignslist = Campaigns(); // declared using List<DataModelCampaigns> mydatamodelcampaignslist;
for (DataModel dm: mydatamodellist) {
for (DataModelCampaigns dmc: mydatamodelcampaignslist) {
Log.i("VALUES",
"Favourite is " + dm.getFAVCurrentLocationLAT() + "(lat) by " + dm.getFAVCurrentLocationLONG() + "(long)" +
"\tCampaign is " + dmc.getCampaignsLAT() + "(lat) by " + dmc.getCampaignsLONG() + "(long)" +
" \n\tDistance is " + (new FavoutiteCampaignLocationDifference(dm,dmc).getDifference()) + " km" //<<<<<<<<<< ADDED
);
}
结果:-
2019-04-30 13:17:00.210 9411-9411/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long) Campaign is 1.1(lat) by 1.1(long)
Distance is 1.0011393900913772E12 km
2019-04-30 13:17:00.210 9411-9411/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long) Campaign is 2.2(lat) by 2.2(long)
Distance is 7.859529733692336E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long) Campaign is 3.3(lat) by 3.3(long)
Distance is 6.306550755502897E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long) Campaign is 4.4(lat) by 4.4(long)
Distance is 5.3532678200287164E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 1.1(lat) by 10.10(long) Campaign is 1.1(lat) by 10.10(long)
Distance is 0.0 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long) Campaign is 1.1(lat) by 1.1(long)
Distance is 7.460634458912574E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long) Campaign is 2.2(lat) by 2.2(long)
Distance is 4.772175072844561E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long) Campaign is 3.3(lat) by 3.3(long)
Distance is 2.6825415833231607E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long) Campaign is 4.4(lat) by 4.4(long)
Distance is 1.1912923776776952E11 km
2019-04-30 13:17:00.211 9411-9411/q.campaigns I/VALUES: Favourite is 6.6(lat) by 6.6(long) Campaign is 1.1(lat) by 10.10(long)
Distance is 5.24682799338737E11 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long) Campaign is 1.1(lat) by 1.1(long)
Distance is 1.0734024701552842E12 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long) Campaign is 2.2(lat) by 2.2(long)
Distance is 7.449624475249066E11 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long) Campaign is 3.3(lat) by 3.3(long)
Distance is 4.764262727131514E11 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long) Campaign is 4.4(lat) by 4.4(long)
Distance is 2.677605323409585E11 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 7.7(lat) by 7.7(long) Campaign is 1.1(lat) by 10.10(long)
Distance is 6.093089156683494E11 km
2019-04-30 13:17:00.212 9411-9411/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long) Campaign is 1.1(lat) by 1.1(long)
Distance is 1.4595734067322017E12 km
2019-04-30 13:17:00.213 9411-9411/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long) Campaign is 2.2(lat) by 2.2(long)
Distance is 1.0716189231163793E12 km
2019-04-30 13:17:00.213 9411-9411/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long) Campaign is 3.3(lat) by 3.3(long)
Distance is 7.43589252928275E11 km
2019-04-30 13:17:00.213 9411-9411/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long) Campaign is 4.4(lat) by 4.4(long)
Distance is 4.754615910210995E11 km
2019-04-30 13:17:00.213 9411-9411/q.campaigns I/VALUES: Favourite is 8.8(lat) by 8.8(long) Campaign is 1.1(lat) by 10.10(long)
Distance is 7.537882177227869E11 km