如何根据用户的当前位置从解析中获取数据?

时间:2016-01-31 18:18:09

标签: java android android-fragments parse-platform google-maps-api-3

我在我的应用程序中从解析中获取一些数据。

我想要的是我想根据用户的当前位置获取数据。我不想在那一刻向他/她展示远离他/她的东西。

这是我到目前为止所做的:

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());
                }
            }
        });
    }

myPointonCreate方法中声明为:

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

注意:此代码全部位于Fragment

问题是在添加这一行:query.whereWithinKilometers("location", myPoint, 5);后,没有任何内容从解析中获取,但在删除之后,数据将被提取并显示在应用程序中!

请让我知道我在这里做错了什么,以及实现我想要达到的目标的最佳途径。

抱歉,如果问题似乎格式错误。我还在学习发表好问题。

1 个答案:

答案 0 :(得分:1)

从您的代码中删除query.addDescendingOrder("createdAt");。它优先于WhereWithin公里。如果问题仍然存在,请尝试使用更大的半径而不是5英里query.whereWithinKilometers("location", myPoint,your_desired_radius");