gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:30:28 26 4
gpt4 key购买 nike

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

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

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com