google maps static map polyline passing through lakes, river, mountains

拥有回忆 提交于 2019-11-28 14:51:17
geocodezip

You need to include the polyline path between each step's start and end points (it is an encoded polyline).

From the directions 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"
     },
     // ...

Including all the step polylines (and encoding the polyline) works for me

jsfiddle creating a URL for the static map from the web service response

BXL

I figured out how to solve it in java.I adapted user geocozip javascript code. In my case, as no waypoints are provided, I just need one leg. So my parse function got this:

  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 re-encode and then put in a format URL readable.

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", ")");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!