Change the unit value of the distance and time getting from map API in android? -


i building android application using google map api distance , time between 2 lat,lng. problem sometime getting value in km , time in meter.

is there way fix unit of distance , time value returning google api. here code.

public class directionsjsonparser {          /** receives jsonobject , returns list of lists containing latitude , longitude */         public list<list<hashmap<string,string>>> parse(jsonobject jobject){      list<list<hashmap<string, string>>> routes = new arraylist<list<hashmap<string,string>>>() ;         jsonarray jroutes = null;             jsonarray jlegs = null;             jsonarray jsteps = null;             jsonobject jdistance = null;             jsonobject jduration = null;      try {          jroutes = jobject.getjsonarray("routes");          /** traversing routes */         for(int i=0;i<jroutes.length();i++){             jlegs = ( (jsonobject)jroutes.get(i)).getjsonarray("legs");             list<hashmap<string, string>> path = new arraylist<hashmap<string, string>>();             /** traversing legs */             for(int j=0;j<jlegs.length();j++){                  /** getting distance json data */                 jdistance = ((jsonobject) jlegs.get(j)).getjsonobject("distance");                 hashmap<string, string> hmdistance = new hashmap<string, string>();                 hmdistance.put("distance", jdistance.getstring("text"));                  /** getting duration json data */                 jduration = ((jsonobject) jlegs.get(j)).getjsonobject("duration");                 hashmap<string, string> hmduration = new hashmap<string, string>();                 hmduration.put("duration", jduration.getstring("text"));                  /** adding distance object path */                 path.add(hmdistance);                  /** adding duration object path */                 path.add(hmduration);                  jsteps = ( (jsonobject)jlegs.get(j)).getjsonarray("steps");                  /** traversing steps */                 for(int k=0;k<jsteps.length();k++){                     string polyline = "";                     polyline = (string)((jsonobject)((jsonobject)jsteps.get(k)).get("polyline")).get("points");                     list<latlng> list = decodepoly(polyline);                     /** traversing points */                     for(int l=0;l<list.size();l++){                         hashmap<string, string> hm = new hashmap<string, string>();                         hm.put("lat", double.tostring(((latlng)list.get(l)).latitude) );                         hm.put("lng", double.tostring(((latlng)list.get(l)).longitude) );                         path.add(hm);                         }                     }                 }             routes.add(path);             }         } catch (jsonexception e) {         e.printstacktrace();         }catch (exception e){         }     return routes;     }     /**      * method decode polyline points      * courtesy : jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java      * */         private list<latlng> decodepoly(string encoded) {     list<latlng> poly = new arraylist<latlng>();     int index = 0, len = encoded.length();     int lat = 0, lng = 0;      while (index < len) {         int b, shift = 0, result = 0;         {             b = encoded.charat(index++) - 63;             result |= (b & 0x1f) << shift;             shift += 5;             } while (b >= 0x20);         int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));         lat += dlat;          shift = 0;         result = 0;         {             b = encoded.charat(index++) - 63;             result |= (b & 0x1f) << shift;             shift += 5;             } while (b >= 0x20);         int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));         lng += dlng;         latlng p = new latlng((((double) lat / 1e5)),         (((double) lng / 1e5)));         poly.add(p);         }     return poly;     } 

here the function distance , time in string.

    /**  * class parse google places in json format  */ private class parsertask extends asynctask<string, integer, list<list<hashmap<string, string>>>> {     // parsing data in non-ui thread     @override     protected list<list<hashmap<string, string>>> doinbackground(string... jsondata) {          jsonobject jobject;         list<list<hashmap<string, string>>> routes = null;          try {             jobject = new jsonobject(jsondata[0]);             directionsjsonparser parser = new directionsjsonparser();              // starts parsing data             routes = parser.parse(jobject);         } catch (exception e) {             e.printstacktrace();         }         return routes;     }       // executes in ui thread, after parsing process      @override       protected void onpostexecute(list<list<hashmap<string, string>>> result) {         arraylist<latlng> points = null;         polylineoptions lineoptions = null;         markeroptions markeroptions = new markeroptions();         string distance = "";         string duration = "";         if (result.size() < 1) {              toast.maketext(getbasecontext(), "no points", toast.length_short).show();             return;         }         // traversing through routes         (int = 0; < result.size(); i++) {             points = new arraylist<latlng>();             lineoptions = new polylineoptions();             // fetching i-th route             list<hashmap<string, string>> path = result.get(i);              // fetching points in i-th route             (int j = 0; j < path.size(); j++) {                 hashmap<string, string> point = path.get(j);                 if (j == 0) {                     // distance list                     distance = (string) point.get("distance");                     continue;                 } else if (j == 1) { // duration list                     duration = (string) point.get("duration");                     continue;                 }                 double lat = double.parsedouble(point.get("lat"));                 double lng = double.parsedouble(point.get("lng"));                 latlng position = new latlng(lat, lng);                  points.add(position);             }              // adding points in route lineoptions             lineoptions.addall(points);             lineoptions.width(10);             lineoptions.color(color.blue);         }          toast.maketext(getapplicationcontext(), "distance:" + distance + ", duration:" + duration, toast.length_long).show();           map.addpolyline(lineoptions);     } } 

i need final return should km in distance , hh:mm in hr.

from code, looks getting results direction web api.

in code,

             /** getting distance json data */             jdistance = ((jsonobject) jlegs.get(j)).getjsonobject("distance");             hashmap<string, string> hmdistance = new hashmap<string, string>();             hmdistance.put("distance", jdistance.getstring("text")); 

notices getting human-readable distance "text", rather integer value value.

look @ piece of json example in documentation

...   },   "distance": {     "value": 2137146,     "text": "1,328 mi"   },   "start_loc... ... 

here, ..leg[0].distance.text human-readable value in miles. should instead integer value ..leg[0].distance.value, in meter.

so in short, value in meter, not text.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -