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

标签 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);,没有从解析中获取任何内容,但在删除它之后,数据被获取并且在应用程序中显示!

请让我知道我在这里做错了什么,以及实现我想要实现的目标的最佳方法是什么。

抱歉,如果问题的格式似乎不正确。我仍在学习发布好问题。

最佳答案

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

关于java - 如何根据用户当前位置从解析中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35117494/

相关文章:

java - 在类路径中查找 StripesResources.properties 的问题

java - 使用 OpenCsv 将 CSV 元素映射到字符串列表?

android - Prop 不会传递给 React Native Navigation v2 中的自定义顶部栏标题组件

android - Android Honeycomb中的 fragment 列表和gridview

android - 从左侧滑动时抽屉导航未打开

java - 如何从我的重定向页面查找导致重定向的网址?

java - 打印正向排序,然后反向排序,直到开始?

android - 在 Android 中查看 holder 类

java - PrintStream不会创建新文件

android - 如果您从 Activity 与 Fragment 调用,是否从 DialogFragment 接收数据?