My program uses google maps directions for web Services to find a route between two points. The result is parsed and stored in variable.
This variable is then used to compose google static map URL.
The parse and the URL are working correctly. The problem is that the drawn "route" passes through a lake and mountains.
{
String GPS = "-22.978823,-43.233249";
String link = MAPS_BASE_URL + "center=brazil," + GPS +
"&markers=color:blue|brazil," + GPS +
"&path=color:0xff0000ff" + "%s" +
"&zoom=13&size=1024x1024&sensor=false";
String htmlContent = "";
String direction_URL= "";
URL url = null;
String parsedStr = null;
Scanner scan = null;
origin = GPS;
destination ="Maracanã";
try {
direction_URL = MAPS_DIRECTIONS_URL;
direction_URL += URLEncoder.encode(origin, "UTF-8");
direction_URL += "&destination=";
direction_URL += URLEncoder.encode(destination, "UTF-8");
direction_URL += "&key=AIzaSyARNFl6ns__p2OEy3uCrZMGem8KW8pXwAI";
}catch(UnsupportedEncodingException e){
Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, e);
}
try {
url = new URL(direction_URL);
} catch (MalformedURLException ex) {
Logger.getLogger(AlertService.class.getName()).log(Level.SEVERE, null, ex);
}
try {
scan = new Scanner(url.openStream());
} catch (IOException ex) {
Logger.getLogger(AlertService.class.getName()).log(Level.SEVERE, null, ex);
}
String str = new String();
while (scan.hasNext())
str += scan.nextLine();
scan.close();
parsedStr = parseJson(str);
try {
InputStream htmlInputStream =
AlertService.class.getResourceAsStream("/resources/gapi.html");
BufferedReader htmlReader = new BufferedReader(
new InputStreamReader(htmlInputStream));
String locationsContent = "";
String wilcardContent = "";
Scanner strScanner = new Scanner(parsedStr);
while (strScanner.hasNextLine())
{
locationsContent = strScanner.nextLine() + "\n";
StringTokenizer st = new StringTokenizer(locationsContent, ";");
if (st.countTokens() == 2)
wilcardContent += "|" + st.nextToken().trim()
+ "," + st.nextToken().trim();
}
link = link.replaceFirst("%s", wilcardContent);
htmlContent = "";
while (htmlReader.ready())
htmlContent += htmlReader.readLine() + "\n";
htmlContent = htmlContent.replaceAll("%s", link);
} catch (FileNotFoundException ex) {
Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
}
return htmlContent;
}
Parse function:
private String parseJson(String s){
String coordinates = new String ();
final JSONObject json = new JSONObject(s);
final JSONObject jsonRoute = json.getJSONArray("routes").getJSONObject(0);
//Get the leg, only one leg as we don't support waypoints
final JSONObject leg = jsonRoute.getJSONArray("legs").getJSONObject(0);
//Get the steps for this leg
final JSONArray steps = leg.getJSONArray("steps");
//Number of steps for use in for loop
final int numSteps = steps.length();
for(int i = 0; i< numSteps; ++i){
final JSONObject step = steps.getJSONObject(i);
final JSONObject startLocation = step.getJSONObject("start_location");
final Double startLat = startLocation.getDouble("lat");
final Double startlng = startLocation.getDouble("lng");
final JSONObject endLocation = step.getJSONObject("end_location");
final Double endtLat = endLocation.getDouble("lat");
final Double endtlng = endLocation.getDouble("lng");
coordinates = coordinates.concat(" ");
coordinates = coordinates.concat(startLat.toString());
coordinates = coordinates.concat(";" + " ");
coordinates = coordinates.concat(startlng.toString());
coordinates = coordinates.concat("\n");
coordinates = coordinates.concat(" ");
coordinates = coordinates.concat(endtLat.toString());
coordinates = coordinates.concat(";" + " ");
coordinates = coordinates.concat(endtlng.toString());
coordinates = coordinates.concat("\n");
}
return coordinates;
}
Json response:
The Final URL look like this: