我有一个简单的应用程序,可以从用户那里获得两个地址并获得行车方向,也可以在地图上显示。
我的问题是:
当用户实际使用该应用时,我应该如何处理该案例,并且他/她不遵循指定的指示?我是说如何检测此事件并获得新的方向?
答案 0 :(得分:0)
正如我们所讨论的,我认为Google map
会Recalculate directions
并根据GPS
上的当前位置将其提供给用户,如果您修复了startAddress
和{{1} }。要获取路线,您需要设置endAddress
以获取Google服务的数据格式,示例代码如下:
AsyncTask
然后获取class GetDirections extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params) {
String startAddress = params[0];
startAddress = startAddress.replaceAll(" ", "%20");
getLatLng(startAddress, false);
String endingAddress = params[1];
endingAddress = endingAddress.replaceAll(" ", "%20");
getLatLng(endingAddress, true);
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
String geoUriString = "http://maps.google.com/maps?addr=" +
addressPos.latitude + "," +
addressPos.longitude + "&daddr=" +
finalAddressPos.latitude + "," +
finalAddressPos.longitude;
Intent mapCall = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUriString));
startActivity(mapCall);
}
}
protected void getLatLng(String address, boolean setDestination) {
String uri = "http://maps.google.com/maps/api/geocode/json?address="
+ address + "&sensor=false";
HttpGet httpGet = new HttpGet(uri);
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int byteData;
while ((byteData = stream.read()) != -1) {
stringBuilder.append((char) byteData);
}
} catch (IOException e) {
e.printStackTrace();
}
double lat = 0.0, lng = 0.0;
JSONObject jsonObject;
try {
jsonObject = new JSONObject(stringBuilder.toString());
lng = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
} catch (JSONException e) {
e.printStackTrace();
}
if (setDestination) {
finalAddressPos = new LatLng(lat, lng);
} else {
addressPos = new LatLng(lat, lng);
}
}
表单start and end address
并在用户editText
时执行AsyncTask:
click get direction button
您可以获取我的Github here的所有源代码。