Google maps static map of a polyline passing through lakes, river, mountains

My program uses Google Maps routes for web services to find a route between two points. The result is analyzed and stored in a variable.
This variable is then used to compile the URL of a google static map.

Analysis and URLs work correctly. The problem is that the drawn "route" passes through the 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; } 

Analysis 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 answer:

https://maps.googleapis.com/maps/api/directions/json?origin=-22.978823,-43.233249&destination=Maracan%C3%A2&key=AIzaSyARNFl6ns__p2OEy3uCrZMGem8KW8pXwAI

The final URL is as follows:

http: //maps.google.com/maps/api/staticmap? center = brazil, -22.978823, -43.233249 & markers = color: blue | brazil, -22.978823, -43.233249 & path = color: 0xff0000ff | -22.9783362, -43.2336781 | -22.9772355, -43.23076390000001 | -22.9772355, -43.23076390000001 | -22.9789792, -43.2300162 | -22.9789792, -43.2300162 | -22.9790772, -43.23036 | -22.9790772, -43.23036 | -22.9786979, -43.22698949999999 | -22.9786979, -43.22698949999999 | -22.9771399 , -43.2196208 | -22.9771399, -43.2196208 | -22.9624962, -43.20396840000001 | -22.9624962, -43.20396840000001 | -22.9583858, -43.2043807 | -22.9583858, -43.2043807 | -22.93445896, -43.2043807 | -22.93445896, -43.2043807 | -22.93445964, -43.2043807 | 43.2096873 | -22.9333061 | -43.2096873 | -22.913577 | -22.9114561, -43.2254838 | -22.9135546, -43.2276606 & zoom = 13 & size = 1024x1024 & sensor = false

-1
source share
2 answers

You need to include the path of the polyline between each step of the beginning and the end points (this is an encoded polyline).

From the response web service response :

  "steps" : [ { "distance" : { "text" : "0.3 km", "value" : 335 }, "duration" : { "text" : "1 min", "value" : 52 }, "end_location" : { "lat" : -22.9772355, "lng" : -43.23076390000001 }, "html_instructions" : "Head \u003cb\u003enortheast\u003c/b\u003e on \u003cb\u003eR. Marquês de São Vicente\u003c/b\u003e", "polyline" : { "points" : "r}fkCna{ fGyBaDMSMSOUMUCMAOOgAEe@Co @ ?o@ ?YAWEk@ ?G" }, "start_location" : { "lat" : -22.9783362, "lng" : -43.2336781 }, "travel_mode" : "DRIVING" }, // ... 

Includes all polyline steps (and coding polyline) for me

jsfiddle creating a static map url from web service response

staticmap? center = brazil, -22.978823, -43.233249 & markers = color: blue | brazil, -22.978823, -43.233249 & path = color: 0xff0000ff | enc: r% 7DfkCna% 7BfGyBaDMSMSOUMUCMAOOgAEe @ Co @% 3Fo @% 3FY% 3FY% 3FY% %FF% @% 3FY% %FF% @% 3FY % 3FDk @ BKDMDKBG @ ATWJKRQRMTIBA% 5EKHCdBc @ D% 3FFAD% 3FD @ F @ F @ BBBBB @ BDBBBFBTDT% 3F% 3FF% 5EBPFP% 3F% 3FBB @ DGGGI @ JGGG @ JGGG @ JGGG @ JGGG @ JGGG @ JGGG @ JGGG @ JGGG @ JGGGJGGGJGG% 5 7BDWoA% 3F% 3FSm @ Qm @ AMCMCWAg @ Cc @ IoAE_ @ QsAG_ @ AEE% 5DSgBS_B% 5BeC% 5BeCM% 7B @ UkBm @ sFOcACKI% 5D% 3F% 3FIMWe @% 5Bc @ QSKEECAECUKEECUKUECEQUSECEQUECEQUKEECU @ O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@ CND% 7C @@ b @ Bj @% 3F% 60 @@% 5CAV% 3F% 5CATE% 60 @ C% 60 @ EXEVKf @ I% 60 @ Ql @ Ql @ Ob @ IXAFCHG% 5CGh @ Eh @ APAJAj @ Ad @ AV @% 5CB% 60 @ BRD% 5EJp @ BN% 3F% 3F @ LL% 7C @ Lz @% 5CfC% 5CzBBPDRBPBJDRFVFRJ% 5EJPHNHNJLnApB% 3F% 3Fh @% 7E @ v @ rAHLHLFLDDFFBDBBRP% 7CC% 7EBzAjA = 1024 & zoom = & 1324 = & zoom = & 1324

0
source

I figured out how to solve it in java.I adapted the geocozip user javascript. In my case, since no waypoints are covered with bone, I just need one foot. So my parsing function got the following:

  List<LatLng> path = new ArrayList(); for(int j = 0; j< numSteps; ++j){ final JSONObject step = steps.getJSONObject(j); final JSONObject polyline = step.getJSONObject("polyline"); final String polylinePoint = polyline.getString("points"); List<LatLng> coordinates = decodePath(polylinePoint); for( int k = 0; k < coordinates.size(); ++k){ path.add(coordinates.get(k)); } } 

It is also necessary to transcode and then put the URL format in a readable format.

 String newPath = path.createPolyLine(encodedPath); String locationsContent=""; locationsContent = URLEncoder.encode(newPath, "UTF-8") .replaceAll("\\%40", "@") .replaceAll("\\+", "%20") .replaceAll("\\%21", "!") .replaceAll("\\%27", "'") .replaceAll("\\%28", "(") .replaceAll("\\%29", ")"); 
0
source

All Articles