0

I'm developing a simple application of public trasport, with buses and stops. The buses doesn't have gps. Every bus stop is an object of type location.

How can I plan the route of every buses along the streets respecting road signs(the distance between 2 bus stop) and how can I go the a bus stop by walk?

Because using

 location1.distanceBetweenTo(location2)

return the distance as the crow flies.

Is it possible to have also the time between 2 location(bus stop)? Because the bus company doesn't have a plan of each stops and time , but the generics time from the departure stop and the arrival stop.

I have already create the map, getting the api key on the google console and I suppose that each bus stop can be represented by a marker.

--EDIT--

This code seems to be working but how create the result?

final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);

JSONArray newTempARr = routes.getJSONArray("legs");
JSONObject newDisTimeOb = newTempARr.getJSONObject(0);

JSONObject distOb = newDisTimeOb.getJSONObject("distance");
JSONObject timeOb = newDisTimeOb.getJSONObject("duration");

Log.i("Distance :", distOb.getString("text"));
Log.i("Time :", timeOb.getString("text"));

I have used some jsonParser but nothing working. How create it starting with latitude and longitude ?

I have already tried in this way.

String url =" https://maps.googleapis.com/maps/api/directions/json?origin=41.221298,16.286054&destination=41.217956,16.2988407&key="my api key"

This string is passed to

 private String getResponseText(String stringUrl) throws IOException
     {
    StringBuilder response  = new StringBuilder();

    URL url = new URL(stringUrl);
    HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
    if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK)
    {
        BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192);
        String strLine = null;
        while ((strLine = input.readLine()) != null)
        {
            response.append(strLine);
        }
        input.close();
    }
    return response.toString();
}

but nothing changes.

Fidelis
  • 91
  • 1
  • 11

1 Answers1

1

This method of getting Json from url works for me

public JSONObject getJson(String url) {
    String responce = null;
    HttpURLConnection conn = null;
    try {
        URL jUrl = new URL(url);
        conn = (HttpURLConnection)jUrl.openConnection();
        InputStream is = (InputStream)conn.getContent();
        java.util.Scanner scanner = new java.util.Scanner(is).useDelimiter("\\A"); 
        responce = scanner.hasNext() ? scanner.next() : "";
        //log.debug( " responce " + responce);
        return new JSONObject(responce);
    } catch (IOException ex){
        String errTxt = "Internet access failure";
        if (conn != null && (ex instanceof java.io.FileNotFoundException))
            try{
                // Read the first line of err message the site possibly returned. Kind of
                // {"cod":401,"message":"Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}
                errTxt = (new BufferedReader(new InputStreamReader(conn.getErrorStream())))
                        .readLine();
                if (errTxt == null)
                    errTxt ="(Server provided no ErrorStream)";
            } catch (Exception ex2){
                log.error(errTxt + " when retrieving ErrorStream", ex2);
            }
        log.error(errTxt, ex);
    } catch (JSONException ex) {
        log.error("Invalid responce from " + url, ex);
    } catch (Exception ex){
        log.error("Failed to get responce from " + url, ex);
    }
    finally {
        if (conn != null)
            conn.disconnect();
    }
    return null;
}
Serg
  • 22,285
  • 5
  • 21
  • 48
  • and how to set the url starting from latitude and logitude? – Fidelis Aug 10 '16 at 12:55
  • ok thanks you now works! How can I show the route on the maps? – Fidelis Aug 10 '16 at 14:25
  • Show it as polyline, see sample polyline sample https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/PolylineDemoActivity.java – Serg Aug 10 '16 at 15:19
  • Ok now works. I've created a new asynctask. I study The polyline and finding a way to draw the path from json. But how can I draw multiple json? The route of a bus from one bus stop to another. – Fidelis Aug 11 '16 at 20:05
  • I'm not sure i understand the architecture of your project. And it will take time and efforts beyond just SO answer to come up with reasonavle advice. You may try collect info from Google maps and store routes at some (local? your site?) database for example. Probably edit this routes according to some factors GM doesn't take into account. – Serg Aug 12 '16 at 06:59
  • The problem now is how to draw a polyline from multiple json object. Because now I have an array of JSON object where each object is obtained by google api direction api from a bus stop to another. If I draw a single json it works, but if draw multiple json, only the last was draw. Using waypoints is a solution, but in the free version google maps api can have max 8 waypoints and I have about 50. – Fidelis Aug 13 '16 at 08:57