英文:
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<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) { // 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 >= 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);
}
}
}
[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
专注分享java语言的经验与见解,让所有开发者获益!
评论