0

my app allows the user to record his way. I use the following code to show the coordinates received on a map:

PolylineOptions rectOptions = new PolylineOptions();

        for (int i = 0; i < _lstLat.length; i++) {
            rectOptions.add(new LatLng(_lstLat[i], _lstLong[i]));
        }

        if (_lstLat.length > 0) {

            LatLng latlng = new LatLng(_lstLat[0], _lstLong[0]);

            CameraUpdate center = CameraUpdateFactory.newLatLng(latlng);
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);

            mMap.moveCamera(center);
            mMap.animateCamera(zoom);

            Polyline polyline = mMap.addPolyline(rectOptions);
        }

Now I want to the distance the user moved. How can I get that? I found the following code that seems to work.

public static String GetDistance(GeoPoint src, GeoPoint dest) throws ProtocolException, IOException, JSONException {

    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json?");
    urlString.append("origin=");// from
    urlString.append(Double.toString((double) src.getLatitudeE6() / 1E6));
    urlString.append(",");
    urlString.append(Double.toString((double) src.getLongitudeE6() / 1E6));
    urlString.append("&destination=");// to
    urlString.append(Double.toString((double) dest.getLatitudeE6() / 1E6));
    urlString.append(",");
    urlString.append(Double.toString((double) dest.getLongitudeE6() / 1E6));
    urlString.append("&mode=walking&sensor=true");
    Log.d("xxx", "URL=" + urlString.toString());

    // get the JSON And parse it to get the directions data.
    HttpURLConnection urlConnection = null;
    URL url = null;

    try {
        url = new URL(urlString.toString());
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.connect();

    InputStream inStream = urlConnection.getInputStream();
    BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));

    String temp, response = "";
    while ((temp = bReader.readLine()) != null) {
        // Parse data
        response += temp;
    }
    // Close the reader, stream & connection
    bReader.close();
    inStream.close();
    urlConnection.disconnect();

    // Sortout JSONresponse
    JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
    JSONArray array = object.getJSONArray("routes");
    // Log.d("JSON","array: "+array.toString());

    // Routes is a combination of objects and arrays
    JSONObject routes = array.getJSONObject(0);
    // Log.d("JSON","routes: "+routes.toString());

    String summary = routes.getString("summary");
    // Log.d("JSON","summary: "+summary);

    JSONArray legs = routes.getJSONArray("legs");
    // Log.d("JSON","legs: "+legs.toString());

    JSONObject steps = legs.getJSONObject(0);
    // Log.d("JSON","steps: "+steps.toString());

    JSONObject distance = steps.getJSONObject("distance");
    // Log.d("JSON","distance: "+distance.toString());

    String sDistance = distance.getString("text");
    int iDistance = distance.getInt("value");

    return sDistance;

}

I want to knwo if this is the best way to accomplish this. Isn't there a way to get this directly from map?

Cheers, Steafn

stefan
  • 1,336
  • 3
  • 21
  • 46
  • If you have the Lat/Lon coordinates you can calculate the distance between 2 points and sum up all distances. http://stackoverflow.com/questions/4813828/distance-between-two-map-point-or-lat-long-in-android – hgoebl Dec 15 '13 at 21:24
  • I have set my gps receiver's position update minDistance to 50 meters. Is this accurate enough when I just calculate the distance between all my points? – stefan Dec 15 '13 at 21:32
  • I tried your suggestion but it seems not to work for me. I tried to sum 200 coordinates (200 because I recorded a way of 50km with positionupdates every 50m)and got a wired result. Used this code for point to point caluclation: float distance = locationA.distanceTo(locationB); – stefan Dec 15 '13 at 22:36
  • a) I think 50m minDistance should create accurate results (when measuring typical car routes, not for walking/hiking). b) weird results - how? completely false values or +/-10%? You should consider that your Gmap-Service is finding a route which might have nothing to do with your real route. – hgoebl Dec 16 '13 at 07:48
  • Offtopic: You could have a look at how easy you can call REST webservices not using `HttpUrlConnection` but with [DavidWebb](https://github.com/hgoebl/DavidWebb). Here is a Gist https://gist.github.com/hgoebl/7984391 – hgoebl Dec 16 '13 at 09:28
  • The "wired" result was caused by a mistake in my implementation. It works when I calculate the distance between the points. Thanks for the assistance!! – stefan Dec 19 '13 at 13:25

0 Answers0