android - JSON and location distance calculate -
i planning this. first device location , info json. json includes latitude , longitude. after, calculate every items distance device. if less x number show in list view. have used use json parsing , list view basic.
and here code location calculate
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);
i use code.
i think this. json items. before on list check distance if lover x put in list else return getting json item
i need codes it.
so have code calculate distance 2 points coordinates? make web page. can save coordinates database, when have phone position can take coordinates near position (like filter). after can use code check witch coordinates inside range x , put class contain coordinates.
your coordinates class:
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; } }
your database 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 need //here // getting coordinates public list<coordinate> getallcoordinate() { list<coordinate> coordinatelist = new arraylist<coordinate>(); // select query string selectquery = "select * " + table_coordinates; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { 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 list coordinatelist.add(coordinate); } while (cursor.movetonext()); } // return coordinate list return coordinatelist; }
with getallcoordinate take coordinate in database , generate array of object can read , access single lat
, lng
. maybe code not perfect if use take care errors. here guide database
Comments
Post a Comment