我正在使用齐射从服务器获取数据并显示在列表视图中。所以我将从服务器上获得客户的地址,我需要计算使用该应用程序的人的当前位置到我从服务器上获得的地址之间的距离。我将获得不同的客户地址,我需要计算他们的所有距离并在列表视图中与客户名称一起单独显示。因此,我使用以下代码。对于所有客户,我得到的距离与图片中所示的相同。这里有地方,thane,德里,kalyan。我需要计算距离从当前位置到那些地方。我不明白问题是什么。使用AyncTask计算距离的代码。此代码在片段内部。
public class getLocationFromNameAsync extends
AsyncTask<String ,String,Double> {
Context context;
onTaskCompleted completed;
public getLocationFromNameAsync(Context context) {
this.context = context;
this.completed = (onTaskCompleted) context;
}
@Override
protected Double doInBackground(String... strings) {
Geocoder coder = new Geocoder(getActivity());
List<Address> address1=null;
List<Address> addresses = null;
try {
address1 = coder.getFromLocationName(district, 5);
} catch (IOException e) {
e.printStackTrace();
}
if (address1 == null) {
Toast.makeText(getActivity(), "Fetching Location,Please wait", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.INVISIBLE);
}
final Address location11 = address1.get(0);
location11.getLatitude();
location11.getLongitude();
if (checkPermission()) {
LocationManager locationManager=(LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location2 = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (location != null) {
latti = location.getLatitude();
longi = location.getLongitude();
} else if (location1 != null) {
latti = location1.getLatitude();
longi = location1.getLongitude();
} else if (location2 != null) {
latti = location2.getLatitude();
longi = location2.getLongitude();
}
coder = new Geocoder(getActivity(), Locale.getDefault());
try {
addresses = coder.getFromLocation(latti, longi, 1);
if (addresses != null && addresses.size() > 0) {
String address = addresses.get(0).getAddressLine(0);
area = addresses.get(0).getLocality();
String city = addresses.get(0).getAdminArea();
String county = addresses.get(0).getCountryName();
String postal_code = addresses.get(0).getPostalCode();
fullAddress=address+","+area+","+city+","+county+","+postal_code;
}
} catch (IOException e) {
e.printStackTrace();
}
}
LatLng source = new LatLng(latti, longi);
LatLng destination = new LatLng(location11.getLatitude(), location11.getLongitude());
double dis= SphericalUtil.computeDistanceBetween(source,destination);
dis/=1000;
return dis;
}
@Override
protected void onPostExecute(Double aDouble) {
completed.onTaskComplete(aDouble);
}
}
我的界面代码。
public interface onTaskCompleted {
public void onTaskComplete(Double result);
}
navigationDrawerActivity代码。在此活动中,我得到结果。
public class navigationDrawer extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener ,onTaskCompleted{
static double distance;
@Override
public void onTaskComplete(Double result) {
distance=result;
Toast.makeText(getApplicationContext(),"result is "+result,Toast.LENGTH_SHORT).show();
}
}
从API获取数据并在listView中设置数据
StringRequest stringRequest = new StringRequest(Request.Method.POST, getTaskUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
arrayList.clear();
try {
JSONObject jo = new JSONObject(response);
JSONArray ja = jo.getJSONArray("dataObj");
int length=ja.length();
for(int i=0; i<length; i++){
JSONObject temp = ja.getJSONObject(i);
nameC= temp.getString("name");
checkId=temp.getString("clientEid");
clientName=temp.getString("client");
candidateFatherName=temp.getString("fatherName");
clientProcess=temp.getString("otherId");
dueDate=temp.getString("deadline");
JSONArray jsaDate = temp.getJSONArray("updateDetails");
for(int k=0; k<jsaDate.length(); k++){
JSONObject jo1 = jsaDate.getJSONObject(k);
sentDate = jo1.getString("date");
}
JSONArray ja1 = temp.getJSONArray("address");
for(int j=0; j<ja1.length(); j++){
JSONObject jo1 = ja1.getJSONObject(j);
landmark=jo1.getString("landmark");
district= jo1.getString("district");
userAddrss=jo1.getString("full");
Geocoder coder = new Geocoder(getActivity());
new getLocationFromNameAsync(getContext()).execute(district);
arrayList.add(new DataModel(nameC,district,landmark,checkId,Math.floor((navigationDrawer.distance*100)/100),clientName,candidateFatherName,clientProcess,sentDate,dueDate));
}
}
答案 0 :(得分:0)
@MarconVasconcelos表示您需要迭代所有列表项,并针对每个列表项应用收到的回复