【发布时间】:2014-08-09 10:56:19
【问题描述】:
我正在编写一个程序,其中我必须显示两个位置之间的计算距离,从当前位置到目标位置,并且我已经编写了一个计算位置之间总距离的方法,现在重点是我在 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 + "");
}
}
}
}
【问题讨论】:
-
您的
locationList为空。 -
不,我正在从 JSON 获取数据
-
但是您没有将其添加到列表中。
-
您在
locationsList.get(position).getName()中获得价值了吗?? -
@SilentKiller 我正在获取列表中的值,请参阅 JSON 中的 doInBacground(...),只是没有获取距离值'
标签: android android-listview gps android-location