Android JSON和位置距离计算
我正在计划这个。第一个设备从json获取所有位置和信息。Json包括纬度和经度。之后,计算每个项目到设备的距离。如果它小于x,则在列表视图中显示它。我曾经使用json解析和列表视图,这是最基本的 这是位置计算的代码Android JSON和位置距离计算,android,json,location,distance,Android,Json,Location,Distance,我正在计划这个。第一个设备从json获取所有位置和信息。Json包括纬度和经度。之后,计算每个项目到设备的距离。如果它小于x,则在列表视图中显示它。我曾经使用json解析和列表视图,这是最基本的 这是位置计算的代码 Location locationA = new Location("point A"); locationA.setLatitude(latA); locationA.setLongitude(lngA); Location locationB = new Loca
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
locationB.setLongitude(lngB);
float distance = locationA.distanceTo(locationB);
我将使用此代码
我想会是这样的。
获取json项。之前但它在列表上检查距离是否大于x将其放入列表中,否则返回获取json项
我需要一些代码。你有用坐标计算两点距离的代码吗? 我用一个网页做了这样的东西。 您可以将所有坐标保存到数据库中,当您拥有手机位置时,您可以取回位置附近的所有坐标(如过滤器)。 之后,您可以使用代码检查坐标是否在X范围内,并将其放入包含坐标的类中 你的职业:
public class Coordinates {
private double _lat;
private double _lng;
//Empty constructor
public Coordinates(){
}
//Constructor
public Coordinates(int id, double lat, double lng){
this._lat = lat;
this._lng = lng;
}
// constructor
public Materia(double lat, double lng){
this._lat = lat;
this._lng = lng;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
//getting latitude
public int getLat(){
return this._lat;
}
//setting color
public void setLat(double lat){
this._lat = lat;
}
//getting longitude
public Double getLng() {
return this._lng;
}
//setting longitude
public void setLng(Double lng) {
this._lng = lng;
}
}
您的数据库Sqlite:
public class MySQLiteHelper extends SQLiteOpenHelper {
//Database version
private static final int DATABASE_VERSION = 1;
//Database name
private static final String DATABASE_NAME = "coordinates.db";
//Materie table name
public static final String TABLE_COORDINATES = "coordinates";
//coordinates columns table names
public static final String COLUMN_ID = "_id";
public static final String COLUMN_LAT = "latitude";
public static final String COLUMN_LNG = "longitude";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
onCreate(){}
onUpdate(){}
//Other methods that you need
//Here something like
// Getting All Coordinates
public List<Coordinate> getAllCoordinate() {
List<Coordinate> coordinateList = new ArrayList<Coordinate>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_COORDINATES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Coordinate coordinate = new Coordinate();
coordinate.setID(Double.parseDouble(cursor.getString(0)));
coordinate.setLat(Double.parseDouble(cursor.getString(1)));
coordinate.setLng(Double.parseDouble(cursor.getString(2)));
// Adding contact to list
coordinateList.add(coordinate);
} while (cursor.moveToNext());
}
// return coordinate list
return coordinateList;
}
公共类MySQLiteHelper扩展了SQLiteOpenHelper{
//数据库版本
私有静态最终int数据库_VERSION=1;
//数据库名称
私有静态最终字符串数据库\u NAME=“coordinates.db”;
//物料表名
公共静态最终字符串表_COORDINATES=“COORDINATES”;
//坐标列表名
公共静态最终字符串列_ID=“_ID”;
公共静态最终字符串列\u LAT=“纬度”;
公共静态最终字符串列_LNG=“经度”;
公共MySQLiteHelper(上下文){
super(上下文、数据库名称、null、数据库版本);
}
onCreate(){}
onUpdate(){}
//您需要的其他方法
//这里有点像
//获取所有坐标
公共列表getAllCoordinate(){
列表坐标列表=新的ArrayList();
//选择所有查询
String selectQuery=“SELECT*FROM”+表格坐标;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
坐标=新坐标();
coordinate.setID(Double.parseDouble(cursor.getString(0));
coordinate.setLat(Double.parseDouble(cursor.getString(1));
coordinate.setLng(Double.parseDouble(cursor.getString(2));
//将联系人添加到列表中
坐标列表。添加(坐标);
}while(cursor.moveToNext());
}
//返回坐标表
回归协调者;
}
使用getAllCoordinate,您可以获取数据库中的所有坐标,并生成一个对象数组,您可以使用for读取该数组,并访问单个lat
和lng
。
如果您在使用时注意一些错误,那么代码可能并不完美。