Use below method to get data from your current location to 40 miles (In radius) from you database:
public void getNearestHotel() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Log.d("FlagLocation", "true");
if (provider != null && !provider.equals("")) {
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
if (location != null) {
double lng = location.getLongitude();
double lat = location.getLatitude();
double kmInLongitudeDegree = 111.320 * Math.cos(lat / 180.0
* Math.PI);
double deltaLat = RADIUS / 111.1;
double deltaLong = RADIUS / kmInLongitudeDegree;
double minLat = lat - deltaLat;
double maxLat = lat + deltaLat;
double minLong = lng - deltaLong;
double maxLong = lng + deltaLong;
HotelHelper helper = new HotelHelper (mActivity); // I have created helper class to get data from database
helper.open();
hotels = helper.getLocationsInLatLongRange(minLat, maxLat,
minLong, maxLong);
helper.close();
} /*else {
HotelHelper helper = new HotelHelper (mActivity);
helper.open();
hotels = helper.getAllHotels();
helper.close();
}*/
} /*else {
HotelHelper helper = new HotelHelper (mActivity);
helper.open();
hotels = helper.getAllHotels();
helper.close();
}*/
}
And here it is the method on how to fetch data from database for the method "getLocationsInLatLongRange(minLat, maxLat,
minLong, maxLong);"
(Remember I have created entity Class as "Hotel" from getter and setter methods)
public ArrayList<Hotel> getLocationsInLatLongRange(double minLat,
double maxLat, double minLong, double maxLong) {
ArrayList<Hotel> hotelLocations = new ArrayList<Hotel>();
//Here You can query on your database to get Hotels from nearby location of given range
String query = "SELECT * " +" FROM " + DATABASE_TABLE + " where " + KEY_LATTITUDE
+ " > " + minLat + " AND " + KEY_LATTITUDE + " < " + maxLat
+ " AND " + KEY_LONGITUDE + " > " + minLong + " AND " + KEY_LONGITUDE + " < " + maxLong
+ " GROUP BY " + KEY_CODE + " ORDER BY " + KEY_CODE;
Cursor c = mDb.rawQuery(query, null);
Log.i("query", c.getCount()+"::"+query);
if (c != null && c.moveToFirst()) {
while (!c.isAfterLast()) {
Airport location = new Airport();
location.setCode(c.getString(c.getColumnIndex(KEY_CODE)));
location.setCountry(c.getString(c.getColumnIndex(KEY_COUNTRY)));
location.setDescription(c.getString(c.getColumnIndex(KEY_DESCRIPTION)));
location.setLattitude(c.getString(c.getColumnIndex(KEY_LATTITUDE)));
location.setLongitude(c.getString(c.getColumnIndex(KEY_LONGITUDE)));
airportLocations.add(location);
c.moveToNext();
}
}
c.close();
return hotelLocations;
}
This way you can have data from you database which will give you result in a hotels which are around 40 miles from your current location
At First you have to find the lat - long of the address of your hotel and you need to update your database and store its lat -long in database accordingly