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

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

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分钟,是正确的:

private void drawRoute(){
    mGoogleApiProvider.getDirections(mOriginLatLng, mDestinationLatLng).enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            try{
                JSONObject jsonObject = new JSONObject(response.body());
                JSONArray jsonArray = jsonObject.getJSONArray("routes");
                JSONObject route = jsonArray.getJSONObject(0);
                JSONObject polylines = route.getJSONObject("overview_polyline");
                String points = polylines.getString("points");
                mPolylineList = DecodePoints.decodePoly(points);
                mPolylineOptions = new PolylineOptions();
                mPolylineOptions.color(Color.DKGRAY);
                mPolylineOptions.width(13f);
                mPolylineOptions.startCap(new SquareCap());
                mPolylineOptions.jointType(JointType.ROUND);
                mPolylineOptions.addAll(mPolylineList);
                mMap.addPolyline(mPolylineOptions);
                JSONObject distance = leg.getJSONObject("distance");
                JSONObject duration = leg.getJSONObject("duration");
                String distanceText = distance.getString("text");
                String durationText = duration.getString("text");
                mTextViewTime.setText(durationText + " " + distanceText);

                String[] distanceAndKm = distanceText.split(" ");
                double distanceValue = Double.parseDouble(distanceAndKm[0]);

                String[] durationValueHoursAndMin = durationText.split(" ");
                double durationValue = Double.parseDouble(durationValueHoursAndMin[2]); 
                double durationValueHours = Double.parseDouble(durationValueHoursAndMin[0]); 
                calculatePrice(distanceValue, durationValue, durationValueHours); 
            } catch (Exception e){
                Log.d("Error", "Error encontrado" + e.getMessage());
            }
        }
    });

    private void calculatePrice(final double distanceValue, final double durationValue, final double durationValueHours) {
        mInfoProvider.getInfo().addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    Info info = dataSnapshot.getValue(Info.class);
                    if (durationValueHours >= 1){
                        double totalDistance = distanceValue * info.getKm();
                        double totalDurationMin = durationValue * info.getMin();
                        double totalDurationHours = durationValueHours * info.getHours();
                        double total = totalDistance + totalDurationMin + totalDurationHours;
                        mTextViewPrice.setText("S/." + total);
                    }
                    else {
                        double totalDistance = distanceValue * info.getKm();
                        double totalDurationMin = durationValue * info.getMin();
                        double total = totalDistance + totalDurationMin;
                        mTextViewPrice.setText("S/." + total);
                    }
                }
            }

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

图片链接

英文:

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,

    private void drawRoute(){
    mGoogleApiProvider.getDirections(mOriginLatLng,mDestinationLatLng).enqueue(new Callback&lt;String&gt;() {
        @Override
        public void onResponse(Call&lt;String&gt; call, Response&lt;String&gt; response) {
            try{
                JSONObject jsonObject = new JSONObject(response.body());
                JSONArray jsonArray =  jsonObject.getJSONArray(&quot;routes&quot;);
                JSONObject route = jsonArray.getJSONObject(0);
                JSONObject polylines = route.getJSONObject(&quot;overview_polyline&quot;);
                String points = polylines.getString(&quot;points&quot;);
                mPolylineList  = DecodePoints.decodePoly(points);
                mPolylineOptions = new PolylineOptions();
                mPolylineOptions.color(Color.DKGRAY);
                mPolylineOptions.width(13f);
                mPolylineOptions.startCap(new SquareCap());
                mPolylineOptions.jointType(JointType.ROUND);
                mPolylineOptions.addAll(mPolylineList);
                mMap.addPolyline(mPolylineOptions);
                JSONObject distance = leg.getJSONObject(&quot;distance&quot;);
                JSONObject duration = leg.getJSONObject(&quot;duration&quot;);
                String distanceText = distance.getString(&quot;text&quot;);
                String durationText = duration.getString(&quot;text&quot;);
                mTextViewTime.setText(durationText + &quot; &quot; + distanceText);

                String[] distanceAndKm = distanceText.split(&quot; &quot;);
                double distanceValue = Double.parseDouble(distanceAndKm[0]);

                String[] durationValueHoursAndMin = durationText.split(&quot; &quot;);
                double durationValue = Double.parseDouble(durationValueHoursAndMin[2]); 
                double durationValueHours = Double.parseDouble(durationValueHoursAndMin[0]); 
                calculatePrice(distanceValue, durationValue,durationValueHours); 
            }catch (Exception e){
                Log.d(&quot;Error&quot;,&quot;Error encontrado&quot; + e.getMessage());
            }

        }

            private void calculatePrice(final double distanceValue, final double durationValue,final double durationValueHours) { //  agregue durationValueHours y modifique todo aca
    mInfoProvider.getInfo().addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){
                Info info = dataSnapshot.getValue(Info.class);
                if (durationValueHours &gt;= 1){
                    double totalDistance = distanceValue * info.getKm();
                    double totalDurationMin = durationValue * info.getMin();
                    double totalDurationHours = durationValueHours * info.getHours();
                    double total = totalDistance + totalDurationMin+totalDurationHours;
                    mTextViewPrice.setText(&quot;S/.&quot; + total);
                }
                else{
                        double totalDistance = distanceValue * info.getKm();
                        double totalDurationMin = durationValue * info.getMin();
                        double total = totalDistance + totalDurationMin ;
                        mTextViewPrice.setText(&quot;S/.&quot; + total);
                }

            }
        }

[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:

确定