首页 文章

如何使用java中的航点准备谷歌 Map api请求

提问于
浏览
0

我正在尝试使用 com.google.maps google库从java准备以下网址 . 我没有找到适当的库方法来添加航点 . 请让我知道如何添加航点到谷歌 Map API .

https://maps.googleapis.com/maps/api/directions/json?origin=17.4366668,78.3982614&destination=17.42955,78.34171&waypoints=via: 17.44027,78.39431|via:17.43149,78.38817&key=AIzaSyDhhwfZgJv4DCVuX-RDuXLXfoHWL6FIPAw

我遵循以下方法将原点和目的地添加到GeoAPi上下文 .

GeoApiContext context = new GeoApiContext();
    context.setApiKey("AIzaSyDhhwfZgJv4DCVuX-RDuXLXfoHWL6FIPAw");

    LatLng originLatLng = new LatLng(17.4366668,78.3982614);

    LatLng destinationLatLng = new LatLng(17.42955,78.34171);

    LatLng wayPoints = new LatLng(17.4477, 78.38264);


    DirectionsResult result = DirectionsApi.newRequest(context)
            .origin(originLatLng)
            .destination(destinationLatLng)
            .waypoints("17.44027,78.39431", "17.43149,78.38817")
            .await();

2 回答

  • 0

    您可以在Github上的源代码中轻松找到路标的方法:

    https://github.com/googlemaps/google-maps-services-java/blob/master/src/main/java/com/google/maps/DirectionsApiRequest.java

    从第151行开始:

    /**
    * Specifies a list of waypoints. Waypoints alter a route by routing it through the specified
    * location(s). A waypoint is specified as either a latitude/longitude coordinate or as an address
    * which will be geocoded. Waypoints are only supported for driving, walking and bicycling
    * directions.
    *
    * <p>For more information on waypoints, see <a href="https://developers.google.com/maps/documentation/directions/#Waypoints">
    * Using Waypoints in Routes</a>.
    */
    public DirectionsApiRequest waypoints(String... waypoints) {
      if (waypoints == null || waypoints.length == 0) {
        return this;
      } else if (waypoints.length == 1) {
        return param("waypoints", waypoints[0]);
      } else {
        return param("waypoints", (optimizeWaypoints ? "optimize:true|" : "") + join('|', waypoints));
      }
    }
    
  • 0

    可以使用LatLng传递航路点参数 . 以下是适合我的代码示例:

    DirectionsResult result = DirectionsApi.newRequest(context)
                    .mode(TravelMode.DRIVING)
                    .origin(new LatLng(-7.372732, 110.50824))
                    .waypoints(new LatLng(-7.272732, 110.508244), new LatLng(-7.172732, 110.508244))
                    .optimizeWaypoints(true)
                    .destination(new LatLng(-7.372732, 110.508244))
                    .awaitIgnoreError();
    

相关问题