user4909407
user4909407

Reputation:

How to fetch data from parse based on user's current location?

I'm fetching some data from parse in my app.

What I want is I want to fetch data based on user's current location. I do not want to show him/her something which is far away from him/her at that moment.

Here is what I have done so far:

public void theMainThing() {

        progressDialog = ProgressDialog.show(getContext(), "",
                "Loading help-requests...", true);

        query = ParseQuery.getQuery("className");
        // added something which should fetch requests according to current location
        query.whereWithinKilometers("location", myPoint, 5); // the geopoints are stored under column named: 'location' on parse.
        query.addDescendingOrder("createdAt");
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> list, ParseException e) {

                if (e == null) {

                    swipeRefreshLayout.setRefreshing(false);
                    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                        @Override
                        public void onRefresh() {
                            swipeRefreshLayout.setRefreshing(true);
                            if (isNetworkAvailable()) {
                                theMainThing();
                            } else {
                                android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getContext());
                                builder.setTitle("No internet connection!");
                                builder.setMessage("Please connect to the internet to refresh the help-requests.");
                                builder.setPositiveButton("Open Settings", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        Intent intent = new Intent(Settings.ACTION_SETTINGS);
                                        startActivity(intent);
                                        swipeRefreshLayout.setRefreshing(false);
                                    }
                                });
                                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        swipeRefreshLayout.setRefreshing(false);
                                    }
                                });
                                builder.show();
                            }
                        }
                    });
                    swipeToRefresh.setVisibility(View.INVISIBLE);

                    Toast.makeText(getContext(), "requests loaded successfully!", Toast.LENGTH_LONG).show();

                    for (int i = 0; i < list.size(); i++) {
                        String id = list.get(i).getObjectId();

                        try {
                            ParseFile parseFile = list.get(i).getParseFile("hImage");
                            byte[] data = parseFile.getData();
                            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                        } catch (Exception err) {
                            err.printStackTrace();
                            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getContext());
                            builder.setTitle("");
                            builder.setMessage(err.getMessage());
                            builder.setPositiveButton(android.R.string.ok, null);
                            builder.show();
                        }

                        hDescriptionAcceptS = (String) list.get(i).get("hDescription");

                        post_timeS = (String) list.get(i).get("currentTime");
                        post_dateS = (String) list.get(i).get("currentDate");
                        posted_byS = (String) list.get(i).get("postedBy");

                        ListContentAAR score = new ListContentAAR(nPicTagAcceptS, bitmap, nDescriptionTagAcceptS, hDescriptionAcceptS, currentCoordinatesTagAcceptS, mapView, R.id.btn_accept, R.id.btn_share, post_dateS, post_timeS, "by " + posted_byS);
                        listContentAARs.add(score);

                        initializeAdapter();

                    }

                } else {
                    progressDialog.dismiss();
                    swipeRefreshLayout.setRefreshing(false);
                    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                        @Override
                        public void onRefresh() {
                            swipeRefreshLayout.setRefreshing(true);
                            theMainThing();
                        }
                    });

                    if (!isNetworkAvailable()) {

                        swipeToRefresh.setText("No internet connection detected!\nConnect to the internet and\nswipe down to refresh.");

                    }

                    swipeToRefresh.setVisibility(View.VISIBLE);

                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getContext());
                    builder.setTitle("");
                    builder.setMessage(e.getMessage());
                    builder.setPositiveButton(android.R.string.ok, null);
                    builder.show();

                    initializeAdapter();

                    Log.d("error retrieving data", e.getMessage());
                }
            }
        });
    }

myPoint is declared in onCreate method as:

Location currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (currentLocation != null) {
                latitude = currentLocation.getLatitude();
                longitude = currentLocation.getLongitude();
            }
            return;
        }
        ParseGeoPoint myPoint = new ParseGeoPoint(latitude, longitude);

Note: This all code is inside a Fragment.

The problem is that after adding this line: query.whereWithinKilometers("location", myPoint, 5);, nothing is getting fetched from parse, but after removing it the data is getting fetched and shown in app!

Please let me know what I am doing wrong here and what is the best way to achieve what I want to achieve.

Sorry, if question seems to be badly formatted. I'm still learning to post good questions.

Upvotes: 0

Views: 99

Answers (1)

Sudip Podder
Sudip Podder

Reputation: 838

remove query.addDescendingOrder("createdAt"); from your code. It takes precedence over whereWithinKilometers. If the problem still occurs, try with bigger radius rather than 5 miles query.whereWithinKilometers("location", myPoint,your_desired_radius");

Upvotes: 1

Related Questions