获取带有距离文本的时间和 JSON,

huangapple 未分类评论52阅读模式
英文:

Get time with distanceText. and JSON ,

问题

以下是你要翻译的内容:

使用JSON和textView获取行程的持续时间,我的问题是,当您请求行程时,会计算持续时间和距离,例如:
1小时22分钟
[0][1][2][3] //
22分钟
[0][1]
但是,如果行程的持续时间少于1小时,只有分钟数,成本为0,我知道是因为我将position[2]放在了分钟数上。我该怎么办?我希望当行程超过59分钟时,分钟数获取位置[2],当行程少于1小时时,获取位置[0]。

在代码的这部分,如果行程大于59分钟,是正确的:

  1. private void drawRoute(){
  2. mGoogleApiProvider.getDirections(mOriginLatLng, mDestinationLatLng).enqueue(new Callback<String>() {
  3. @Override
  4. public void onResponse(Call<String> call, Response<String> response) {
  5. try{
  6. JSONObject jsonObject = new JSONObject(response.body());
  7. JSONArray jsonArray = jsonObject.getJSONArray("routes");
  8. JSONObject route = jsonArray.getJSONObject(0);
  9. JSONObject polylines = route.getJSONObject("overview_polyline");
  10. String points = polylines.getString("points");
  11. mPolylineList = DecodePoints.decodePoly(points);
  12. mPolylineOptions = new PolylineOptions();
  13. mPolylineOptions.color(Color.DKGRAY);
  14. mPolylineOptions.width(13f);
  15. mPolylineOptions.startCap(new SquareCap());
  16. mPolylineOptions.jointType(JointType.ROUND);
  17. mPolylineOptions.addAll(mPolylineList);
  18. mMap.addPolyline(mPolylineOptions);
  19. JSONObject distance = leg.getJSONObject("distance");
  20. JSONObject duration = leg.getJSONObject("duration");
  21. String distanceText = distance.getString("text");
  22. String durationText = duration.getString("text");
  23. mTextViewTime.setText(durationText + " " + distanceText);
  24. String[] distanceAndKm = distanceText.split(" ");
  25. double distanceValue = Double.parseDouble(distanceAndKm[0]);
  26. String[] durationValueHoursAndMin = durationText.split(" ");
  27. double durationValue = Double.parseDouble(durationValueHoursAndMin[2]);
  28. double durationValueHours = Double.parseDouble(durationValueHoursAndMin[0]);
  29. calculatePrice(distanceValue, durationValue, durationValueHours);
  30. } catch (Exception e){
  31. Log.d("Error", "Error encontrado" + e.getMessage());
  32. }
  33. }
  34. });
  35. private void calculatePrice(final double distanceValue, final double durationValue, final double durationValueHours) {
  36. mInfoProvider.getInfo().addListenerForSingleValueEvent(new ValueEventListener() {
  37. @Override
  38. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  39. if (dataSnapshot.exists()){
  40. Info info = dataSnapshot.getValue(Info.class);
  41. if (durationValueHours >= 1){
  42. double totalDistance = distanceValue * info.getKm();
  43. double totalDurationMin = durationValue * info.getMin();
  44. double totalDurationHours = durationValueHours * info.getHours();
  45. double total = totalDistance + totalDurationMin + totalDurationHours;
  46. mTextViewPrice.setText("S/." + total);
  47. }
  48. else {
  49. double totalDistance = distanceValue * info.getKm();
  50. double totalDurationMin = durationValue * info.getMin();
  51. double total = totalDistance + totalDurationMin;
  52. mTextViewPrice.setText("S/." + total);
  53. }
  54. }
  55. }

在这张图片中,你可以看到带有小时、分钟以及仅分钟的价格:

图片链接

英文:

Im using JSON and textView to get the duration of the trip , my question is that , when you request a trip , these calculate the duration and distance , for example
: 1 h 22 m
[0][1][2][3] //
22 min
[0][1]
but if the duration of the trip is less than a hour, just minutes , the price of the cost is 0 , i know is because i put the position[2] in the minutes.What can i do? i want when trip is more than a 59 min, minute get position [2] and when is less than a hour get position [0].

in this part of code is fine if the trip is > 59 min,

  1. private void drawRoute(){
  2. mGoogleApiProvider.getDirections(mOriginLatLng,mDestinationLatLng).enqueue(new Callback&lt;String&gt;() {
  3. @Override
  4. public void onResponse(Call&lt;String&gt; call, Response&lt;String&gt; response) {
  5. try{
  6. JSONObject jsonObject = new JSONObject(response.body());
  7. JSONArray jsonArray = jsonObject.getJSONArray(&quot;routes&quot;);
  8. JSONObject route = jsonArray.getJSONObject(0);
  9. JSONObject polylines = route.getJSONObject(&quot;overview_polyline&quot;);
  10. String points = polylines.getString(&quot;points&quot;);
  11. mPolylineList = DecodePoints.decodePoly(points);
  12. mPolylineOptions = new PolylineOptions();
  13. mPolylineOptions.color(Color.DKGRAY);
  14. mPolylineOptions.width(13f);
  15. mPolylineOptions.startCap(new SquareCap());
  16. mPolylineOptions.jointType(JointType.ROUND);
  17. mPolylineOptions.addAll(mPolylineList);
  18. mMap.addPolyline(mPolylineOptions);
  19. JSONObject distance = leg.getJSONObject(&quot;distance&quot;);
  20. JSONObject duration = leg.getJSONObject(&quot;duration&quot;);
  21. String distanceText = distance.getString(&quot;text&quot;);
  22. String durationText = duration.getString(&quot;text&quot;);
  23. mTextViewTime.setText(durationText + &quot; &quot; + distanceText);
  24. String[] distanceAndKm = distanceText.split(&quot; &quot;);
  25. double distanceValue = Double.parseDouble(distanceAndKm[0]);
  26. String[] durationValueHoursAndMin = durationText.split(&quot; &quot;);
  27. double durationValue = Double.parseDouble(durationValueHoursAndMin[2]);
  28. double durationValueHours = Double.parseDouble(durationValueHoursAndMin[0]);
  29. calculatePrice(distanceValue, durationValue,durationValueHours);
  30. }catch (Exception e){
  31. Log.d(&quot;Error&quot;,&quot;Error encontrado&quot; + e.getMessage());
  32. }
  33. }
  34. private void calculatePrice(final double distanceValue, final double durationValue,final double durationValueHours) { // agregue durationValueHours y modifique todo aca
  35. mInfoProvider.getInfo().addListenerForSingleValueEvent(new ValueEventListener() {
  36. @Override
  37. public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  38. if (dataSnapshot.exists()){
  39. Info info = dataSnapshot.getValue(Info.class);
  40. if (durationValueHours &gt;= 1){
  41. double totalDistance = distanceValue * info.getKm();
  42. double totalDurationMin = durationValue * info.getMin();
  43. double totalDurationHours = durationValueHours * info.getHours();
  44. double total = totalDistance + totalDurationMin+totalDurationHours;
  45. mTextViewPrice.setText(&quot;S/.&quot; + total);
  46. }
  47. else{
  48. double totalDistance = distanceValue * info.getKm();
  49. double totalDurationMin = durationValue * info.getMin();
  50. double total = totalDistance + totalDurationMin ;
  51. mTextViewPrice.setText(&quot;S/.&quot; + total);
  52. }
  53. }
  54. }

[enter image description here][1]

in this image you can see the price with hours minutes and with just minutes
[1]: https://i.stack.imgur.com/CM9OO.png

huangapple
  • 本文由 发表于 2020年7月25日 03:36:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/63080387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定