【问题标题】:JSON and location distance calculateJSON和位置距离计算
【发布时间】:2015-04-01 11:45:36
【问题描述】:

我正在计划这个。第一个设备从 json 获取所有位置和信息。 Json 包括纬度和经度。之后,计算每个项目到设备的距离。如果它小于 x 数,则在列表视图中显示它。我曾经使用过基本的 json 解析和列表视图。

这是位置计算的代码

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 项目

我需要一些代码。

【问题讨论】:

    标签: android json location distance


    【解决方案1】:

    那么你有用坐标计算两点距离的代码吗? 我用网页制作了这样的东西。 您可以将所有坐标保存到数据库中,当您拥有手机位置时,您可以取回您位置附近的所有坐标(如过滤器)。 之后,您可以使用您的代码检查女巫坐标是否在您的范围 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;
    }
    

    使用 getAllCoordinate 您可以获取数据库中的所有坐标并生成一个对象数组,您可以使用 for 读取并访问单个 latlng。 如果您使用它注意一些错误,那么代码可能并不完美。 Here a good guide for database

    【讨论】:

      猜你喜欢
      • 2011-04-03
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多