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

后端 未结 2 1894
小鲜肉
小鲜肉 2020-12-12 07:32

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 co

相关标签:
2条回答
  • 2020-12-12 08:30

    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

    0 讨论(0)
  • 2020-12-12 08:33

    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", ")");
    
    0 讨论(0)
提交回复
热议问题