我一直在尝试使用距离矩阵API来获取两个位置之间的步行时间和距离。我想将此结果(距离和持续时间)存储在可以从其他类/活动访问的变量中。
我设法使用AsyncTask获得时间和持续时间,但是当我尝试将它们存储在PostExecute方法中的变量中时,从PostExecute方法之外的方法或类进行访问时,它们始终是未分配的。我现在了解AsyncTask的工作原理,并且它是异步的。我尝试实现接口,然后尝试访问数据,但仍然无法。
GeoTask是我的Map类中的一个内部类。
public class GeoTask extends AsyncTask<String, Void, String> {
ProgressDialog pd;
Context mContext;
SelectTime selectTime;
//constructor is used to get the context.
public GeoTask(Context mContext) {
this.mContext = mContext;
}
public GeoTask() {
}
//This function is executed before before "doInBackground(String...params)" is executed to dispaly the progress dialog
@Override
protected void onPreExecute() {
super.onPreExecute();
selectTime=new SelectTime();
pd=new ProgressDialog(mContext);
pd.setMessage("Loading");
pd.setCancelable(false);
pd.show();
}
//This function is executed after the execution of "doInBackground(String...params)" to dismiss the dispalyed progress dialog and call "setDouble(Double)" defined in "MainActivity.java"
@Override
protected void onPostExecute(String aDouble) {
super.onPostExecute(aDouble);
if(aDouble!=null)
{
setDouble(aDouble);
}
else
Toast.makeText(mContext, "Error4!Please Try Again with proper values", Toast.LENGTH_SHORT).show();
}
@Override
protected String doInBackground(String... params) {
try {
URL url=new URL(params[0]);
HttpURLConnection con= (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
int statuscode=con.getResponseCode();
if(statuscode==HttpURLConnection.HTTP_OK)
{
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb=new StringBuilder();
String line=br.readLine();
while(line!=null)
{
sb.append(line);
line=br.readLine();
}
String json=sb.toString();
Log.d("JSON",json);
JSONObject root=new JSONObject(json);
JSONArray array_rows=root.getJSONArray("rows");
Log.d("JSON","array_rows:"+array_rows);
JSONObject object_rows=array_rows.getJSONObject(0);
Log.d("JSON","object_rows:"+object_rows);
JSONArray array_elements=object_rows.getJSONArray("elements");
Log.d("JSON","array_elements:"+array_elements);
JSONObject object_elements=array_elements.getJSONObject(0);
Log.d("JSON","object_elements:"+object_elements);
JSONObject object_duration=object_elements.getJSONObject("duration");
JSONObject object_distance=object_elements.getJSONObject("distance");
Log.d("JSON","object_duration:"+object_duration);
return object_duration.getString("value")+","+object_distance.getString("value");
}
} catch (MalformedURLException e) {
Log.d("error", "error1");
} catch (IOException e) {
Log.d("error", "error2");
} catch (JSONException e) {
Log.d("error","error3");
}
return null;
}
}
在Map类中,使用setDouble方法:
public void setDouble(String result) {
String res[] = result.split(",");
Double m = Double.parseDouble(res[0]) / 60;
Double d = Double.parseDouble(res[1])/1000;
minutes=m
distance=d
}
分钟和距离变量的初始化
private double minutes;
public double getMinutes() {
return minutes;
}
private double distance;
public double getDistance() {
return distance;
}
和执行:
LatLng destinationLatLng = getLatLngFromAddress(destinationPassed);
currentOrigin = getAddressFromLatLng(currentLat,currentLong);
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + currentOrigin + "&destinations=" + destinationPassed + "&mode=walking&language=fr-FR&avoid=tolls&key=API_KEY";
Log.d("url string",url);
geoTask.execute(url);
此外,即使在PostExecution完成后,我也始终使用返回的值0.0来获取分钟和距离变量,即使在PostExecution完成后,我也始终获得值0.0(我使用Logs测试了它是否完成)
1)如何实现我想要的?访问API在不同类中返回的距离和持续时间?这对于我正在实现的应用程序至关重要。
2)如果使用AsyncTask和此方法无法实现所需的功能,是否可以不使用AsyncTask来使用Distance Matrix API?
可能您应该替换为您的>]
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + currentOrigin + "&destinations=" + destinationPassed + "&mode=walking&language=fr-FR&avoid=tolls&key=API_KEY";