|
15 | 15 | import com.mapbox.api.directions.v5.utils.ParseUtils; |
16 | 16 | import com.mapbox.geojson.Point; |
17 | 17 | import java.io.UnsupportedEncodingException; |
| 18 | +import java.net.MalformedURLException; |
18 | 19 | import java.net.URL; |
19 | 20 | import java.net.URLDecoder; |
20 | 21 | import java.util.List; |
@@ -844,6 +845,59 @@ public static RouteOptions fromUrl(@NonNull URL url) { |
844 | 845 | return fromJsonString(optionsJson.toString()); |
845 | 846 | } |
846 | 847 |
|
| 848 | + /** |
| 849 | + * Create a URL from RouteOptions instance. |
| 850 | + * |
| 851 | + * @param accessToken access token to make API request |
| 852 | + * @return a URL object instance |
| 853 | + */ |
| 854 | + @NonNull |
| 855 | + public URL toUrl(@NonNull String accessToken) { |
| 856 | + StringBuilder sb = new StringBuilder() |
| 857 | + .append(baseUrl()) |
| 858 | + .append("/directions/v5") |
| 859 | + .append(String.format("/%s", user())) |
| 860 | + .append(String.format("/%s", profile())) |
| 861 | + .append(String.format("/%s", coordinates())) |
| 862 | + .append(String.format("?access_token=%s", accessToken)) |
| 863 | + .append(String.format("&alternatives=%s", alternatives())) |
| 864 | + .append(String.format("&geometries=%s", geometries())) |
| 865 | + .append(String.format("&overview=%s", overview())) |
| 866 | + .append(String.format("&radiuses=%s", radiuses())) |
| 867 | + .append(String.format("&steps=%s", steps())) |
| 868 | + .append(String.format("&bearings=%s", bearings())) |
| 869 | + .append(String.format("&layers=%s", layers())) |
| 870 | + .append(String.format("&continue_straight=%s", continueStraight())) |
| 871 | + .append(String.format("&annotations=%s", annotations())) |
| 872 | + .append(String.format("&language=%s", language())) |
| 873 | + .append(String.format("&roundabout_exits=%s", roundaboutExits())) |
| 874 | + .append(String.format("&voice_instructions=%s", voiceInstructions())) |
| 875 | + .append(String.format("&banner_instructions=%s", bannerInstructions())) |
| 876 | + .append(String.format("&voice_units=%s", voiceUnits())) |
| 877 | + .append(String.format("&exclude=%s", exclude())) |
| 878 | + .append(String.format("&include=%s", include())) |
| 879 | + .append(String.format("&approaches=%s", approaches())) |
| 880 | + .append(String.format("&waypoints=%s", waypointIndices())) |
| 881 | + .append(String.format("&waypoint_names=%s", waypointNames())) |
| 882 | + .append(String.format("&waypoint_targets=%s", waypointTargets())) |
| 883 | + .append(String.format("&enable_refresh=%s", enableRefresh())) |
| 884 | + .append(String.format("&walking_speed=%s", walkingSpeed())) |
| 885 | + .append(String.format("&walkway_bias=%s", walkwayBias())) |
| 886 | + .append(String.format("&alley_bias=%s", alleyBias())) |
| 887 | + .append(String.format("&snapping_include_closures=%s", snappingIncludeClosures())) |
| 888 | + .append(String.format("&arrive_by=%s", arriveBy())) |
| 889 | + .append(String.format("&depart_at=%s", departAt())) |
| 890 | + .append(String.format("&max_height=%s", maxHeight())) |
| 891 | + .append(String.format("&max_width=%s", maxWidth())) |
| 892 | + .append(String.format("&metadata=%s", metadata())); |
| 893 | + |
| 894 | + try { |
| 895 | + return new URL(sb.toString()); |
| 896 | + } catch (MalformedURLException ex) { |
| 897 | + throw new RuntimeException(ex); |
| 898 | + } |
| 899 | + } |
| 900 | + |
847 | 901 | @NonNull |
848 | 902 | private static RouteOptions fromJsonString(@NonNull String json) { |
849 | 903 | GsonBuilder gson = new GsonBuilder(); |
|
0 commit comments