我正在编写一个程序,其中我必须显示两个位置之间的计算距离,从当前位置到目标位置,并得到我已经编写了一个计算位置之间的总距离的方法,现在主要的点是我我没有在TextView中获得距离值。
这是我的代码,请查看以下内容:
LocationsActivity.java:
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(LocationsActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("locations");
Log.d("jarray", jarray.toString());
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
locations = new Locations();
locations.setName(object.getString("name"));
locations.setLatitude(object.getString("latitude"));
locations.setLongitude(object.getString("longitude"));
actorsList.add(locations);
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
adapter.notifyDataSetChanged();
if(result == false)
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
public void distanceBetweenTwoLocations() {
Location currentLocation = new Location("");
currentLocation.setLatitude(latitude);
currentLocation.setLongitude(longitude);
for (int i = 0; i < actorsList.size(); i++) {
Location destinationLocation = new Location(" ");
destinationLocation.setLatitude(Double.valueOf(actorsList.get(i).getLatitude()));
destinationLocation.setLongitude(Double.valueOf(actorsList.get(i).getLongitude()));
double inMeters = currentLocation.distanceTo(destinationLocation);
double totalDistance = inMeters / 1000;
locations.setDistance(totalDistance + "");
}
}
}
}
答案 0 :(得分:2)
问题在于您的distanceBetweenTwoLocations()
方法。
当您从actorsList
locationsList
中添加位置数据
你的getView方法仍然存在同样的错误。您在Locations
列表中添加数据,而在getView中,您从locationsList
已更新:
在您为actorsList
列表分配距离时,您正在将locations
传递给适配器。
使用以下代码检查:
在通知适配器onPostExecute
adapter.notifyDataSetChanged();
调用以下方法
public void distanceBetweenTwoLocations() {
Location currentLocation = new Location("");
currentLocation.setLatitude(latitude);
currentLocation.setLongitude(longitude);
for (int i = 0; i < actorsList.size(); i++) {
Locations mLocations = (Locations) actorList.get(i);
Location destinationLocation = new Location(" ");
destinationLocation.setLatitude(Double.valueOf(actorsList.get(i).getLatitude()));
destinationLocation.setLongitude(Double.valueOf(actorsList.get(i).getLongitude()));
double inMeters = currentLocation.distanceTo(destinationLocation);
double totalDistance = inMeters / 1000;
mLocations.setDistance(totalDistance + "");
}
}
}