0

I have some shops addresses in my database. What I want to achieve is to search the nearby shop around the user's dynamically fetched location. I successfully fetched user's lat and long through Google's API using Location Manager. But I am unable to proceed further. If any body have some Idea please guide me.

for reference you can refer the near by feature in Zomato App https://play.google.com/store/apps/details?id=com.application.zomato&hl=en it has same feature what I exactly want in my app.

This may not be difficult and found in many app, But after much research I didn't get any clue to work around.

2 Answers2

1

May this help you.

you can achieve this by finding distance b/w two location. e.g in web service you get x,y lat-long and radius 5km.

get All Location from database and find distance b.w x,y and location db. if distance <=5km then return to user.

Calculating distance between two points, using latitude longitude, what am I doing wrong?

Community
  • 1
  • 1
Manjeet Brar
  • 914
  • 1
  • 9
  • 27
  • Very good suggestion. I will surly implement this but, my database is a huge one and very old and didn't have lat long information. It, only contains the addresses of shops. So, I can't implement this. Is there any other way to do same. – Gaurav Gupta Sep 13 '14 at 09:22
  • this is only the way to achieve. There have another way you can find lat long of your locations by address using Google APIS and store into database. https://developers.google.com/maps/documentation/geocoding/ – Manjeet Brar Sep 13 '14 at 09:24
  • Ok, Thanks I will go for this one – Gaurav Gupta Sep 13 '14 at 09:33
0

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

Jigar
  • 791
  • 11
  • 21