Follow

Creating json itinerary with time windows example

Description

This example demonstrates how to send to navigation the itinerary with a json object (SendJsonItinerary) and subsequently to apply status of the itinerary execution (ReadJsonItineraryStatus) anytime.
We feed this example with 3 waypoints, each defined with Gps coordinates, time windows and type of the waypoint.
When retrieving the execution status we can read out e.g. the information about the number of visited and unvisited waypoints, and then per each waypoint more detailed info such as ETA, remaining distance and time to the waypoint, etc.
In the example we used the json library "json.org", which offers convenient manipulations with json strings and objects for construction and parsing.
For the definition of Sygic Json strings please refer to Json itinerary specification
Please note the json API functions (addItinerary, getRouteStatus) are only available with 3D versions of navigation.

Java

	import org.json.JSONArray;
	import org.json.JSONException;
	import org.json.JSONObject;
	...

	void SendJsonItinerary()
	{
		String itname = "itin-63";
		ArrayList<TwWaypoint> lst = new ArrayList<TwWaypoint>();
		lst.add(new TwWaypoint(48.11468, 17.13110, "2015-06-28T16:00:00Z", "2015-06-28T16:00:00Z", "start"));
		lst.add(new TwWaypoint(48.13067, 17.16062, "2015-06-28T17:00:00Z", "2015-06-28T17:30:00Z", "via"));
		lst.add(new TwWaypoint(48.10039, 17.14722, "2015-06-28T18:00:00Z", "2015-06-28T18:30:00Z", "finish"));

		String json = CreateJsonItinerary(lst);
		try {
			ApiItinerary.addItinerary(json, itname, 0);
			ApiItinerary.setRoute(itname, 0, 0);
		} catch (GeneralException e) {
			e.printStackTrace();
		}
	}

	void ReadJsonItineraryStatus()
	{
		String output = GetJsonRouteStatus();
		Logme(output);
	}

	// Json construction and parsing support functions

	JSONObject createWaypoint(double lat, double lon, String time1, String time2, int waypointId, String wtype)
	{
		int ilat = (int)(lat * 100000);
		int ilon = (int)(lon * 100000);

		JSONObject obj = new JSONObject();
		try {
		   obj.put("lat", Integer.valueOf(ilat));
		   obj.put("lon", Integer.valueOf(ilon));
		   obj.put("waypointId", Integer.valueOf(waypointId));
		   obj.put("hitRadius", Integer.valueOf(200));
		   obj.put("type", wtype);
		   if (time1 != null && time2 != null)
		   {
			   JSONObject tw = new JSONObject();
			   tw.put("startTime", time1);
			   tw.put("endTime", time2);
			   tw.put("stopDelay", Integer.valueOf(180));
			   obj.put("timeWindow", tw);
		   }
		}
		catch (JSONException e)
		{
			obj = null;
		}
		return obj;
	}

	JSONObject createRoutepart(JSONObject wpFrom, JSONObject wpTo)
	{
		JSONObject obj = new JSONObject();
		try {
		   if (wpFrom != null) obj.put("waypointFrom", wpFrom);
		   obj.put("waypointTo", wpTo);
		}
		catch (JSONException e)
		{
			obj = null;
		}
		return obj;
	}


	public class TwWaypoint {
		double lat;
		double lon;
		String dtStart;
		String dtEnd;
		String wpType;
		public TwWaypoint (double lat, double lon, String dtStart, String dtEnd, String wpType)
		{
			this.lat = lat;
			this.lon = lon;
			this.dtStart = dtStart;
			this.dtEnd = dtEnd;
			this.wpType = wpType;
		}
	}

	protected String CreateJsonItinerary(ArrayList<TwWaypoint> lst)
	{
		try {
			JSONArray routeparts = new JSONArray();
			JSONObject routepart;
			JSONObject wpFrom;
			JSONObject wpTo;
			for (int i = 1; i < lst.size(); i++)
			{
			   wpFrom = null;
			   if (i == 1) wpFrom = createWaypoint(lst.get(0).lat, lst.get(0).lon, lst.get(0).dtStart, lst.get(0).dtEnd, 0, lst.get(0).wpType);
			   wpTo = createWaypoint(lst.get(i).lat, lst.get(i).lon, lst.get(i).dtStart, lst.get(i).dtEnd, i, lst.get(i).wpType);
			   routepart = createRoutepart(wpFrom, wpTo);
			   if (routepart == null) return null;
			   routeparts.put(routepart);
			}
			JSONObject json = new JSONObject();
			json.put("name", "Bratislava-pickup");
			json.put("version", "2.2");
			json.put("routeParts", routeparts);
			return json.toString();
		} catch (JSONException e) {
			  e.printStackTrace();
		}
		return null;
   }


   protected String GetJsonRouteStatus()
   {
		try {
			String ret = ApiNavigation.getRouteStatus(0);

			JSONObject jout = new JSONObject(ret);
			String n1 = jout.getString("numVisited");                   // retrieve overall stats
			String n2 = jout.getString("numUnvisited");
			String n3 = jout.getString("numPredictedLateArrivals");
			String n4 = jout.getString("numPredictedEarlyArrivals");
			JSONArray jArr = jout.getJSONArray("waypoints");

			String output = "visited/unvisited/early/late:" + n1 + "/" + n2 + "/" + n3 + "/" + n4 + "\n";
			for (int i=0; i < jArr.length(); i++)
			{
				JSONObject obj = jArr.getJSONObject(i);
				String waypointId = obj.getString("waypointId");              // retrieve waypoint id
				JSONObject rts = obj.getJSONObject("realtimeStatus");
				String status = rts.getString("status");                      // retrieve status
				String eta = "n/a";
				int drem = -1;
				int trem = -1;
				if (status.compareTo("unvisited") == 0)
				{
				   eta = rts.getString("estimatedTimeArrival");     // retrieve eta to the waypoint
				   drem = rts.getInt("distanceRemaining");          // retrieve remaining distance in meters
				   trem = rts.getInt("timeRemaining");              // retrieve remaining time in seconds
				}
				output += "waypointId=" + waypointId + ",";
				output += "status=" + status + "\n";
				output += "eta=" + eta + "\n";
				output += "distanceRemaining=" + String.valueOf(drem) + "\n";
				output += "timeRemaining=" + String.valueOf(trem) + "\n";
			}
			return output;

		} catch (GeneralException e) {
			e.printStackTrace();
		}
		catch (JSONException e) {
			e.printStackTrace();
		}

		return null;
   }
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments