|
16 | 16 | package com.google.maps.model; |
17 | 17 |
|
18 | 18 | import com.google.maps.internal.PolylineEncoding; |
| 19 | +import com.google.maps.internal.StringJoin; |
| 20 | + |
19 | 21 | import java.io.Serializable; |
| 22 | +import java.util.ArrayList; |
20 | 23 | import java.util.List; |
21 | 24 |
|
22 | 25 | /** |
23 | 26 | * Encoded Polylines are used by the API to represent paths. |
24 | 27 | * |
25 | | - * <p>See <a href="https://developers.google.com/maps/documentation/utilities/polylinealgorithm"> |
| 28 | + * <p> |
| 29 | + * See <a href= |
| 30 | + * "https://developers.google.com/maps/documentation/utilities/polylinealgorithm"> |
26 | 31 | * Encoded Polyline Algorithm</a> for more detail on the protocol. |
27 | 32 | */ |
28 | 33 | public class EncodedPolyline implements Serializable { |
29 | 34 |
|
30 | | - private static final long serialVersionUID = 1L; |
| 35 | + private static final long serialVersionUID = 1L; |
| 36 | + |
| 37 | + private int weight; |
| 38 | + private String color; |
| 39 | + private String fillcolor; |
| 40 | + private boolean geodesic; |
| 41 | + private final String points; |
| 42 | + |
| 43 | + public EncodedPolyline() { |
| 44 | + this.points = null; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param encodedPoints A string representation of a path, encoded with the |
| 49 | + * Polyline Algorithm. |
| 50 | + */ |
| 51 | + public EncodedPolyline(String encodedPoints) { |
| 52 | + this.points = encodedPoints; |
| 53 | + } |
| 54 | + |
| 55 | + /** @param points A path as a collection of {@code LatLng} points. */ |
| 56 | + public EncodedPolyline(List<LatLng> points) { |
| 57 | + this.points = PolylineEncoding.encode(points); |
| 58 | + } |
| 59 | + |
| 60 | + public String getEncodedPath() { |
| 61 | + return toUrlValue(); |
| 62 | + } |
| 63 | + |
| 64 | + public List<LatLng> decodePath() { |
| 65 | + return PolylineEncoding.decode(points); |
| 66 | + } |
| 67 | + |
| 68 | + // Use the encoded point representation; decoding to get an alternate |
| 69 | + // representation for |
| 70 | + // individual points would be expensive. |
| 71 | + @Override |
| 72 | + public String toString() { |
| 73 | + return String.format("[EncodedPolyline: %s]", points); |
| 74 | + } |
| 75 | + |
| 76 | + public int getWeight() { |
| 77 | + return weight; |
| 78 | + } |
| 79 | + |
| 80 | + public void setWeight(int weight) { |
| 81 | + this.weight = weight; |
| 82 | + } |
| 83 | + |
| 84 | + public String getColor() { |
| 85 | + return color; |
| 86 | + } |
| 87 | + |
| 88 | + public void setColor(String color) { |
| 89 | + this.color = color; |
| 90 | + } |
| 91 | + |
| 92 | + public String getFillcolor() { |
| 93 | + return fillcolor; |
| 94 | + } |
| 95 | + |
| 96 | + public void setFillcolor(String fillcolor) { |
| 97 | + this.fillcolor = fillcolor; |
| 98 | + } |
| 99 | + |
| 100 | + public boolean isGeodesic() { |
| 101 | + return geodesic; |
| 102 | + } |
| 103 | + |
| 104 | + public void setGeodesic(boolean geodesic) { |
| 105 | + this.geodesic = geodesic; |
| 106 | + } |
31 | 107 |
|
32 | | - private final String points; |
| 108 | + public String toUrlValue() { |
| 109 | + List<String> urlParts = new ArrayList<>(); |
33 | 110 |
|
34 | | - public EncodedPolyline() { |
35 | | - this.points = null; |
36 | | - } |
| 111 | + if (weight > 0) { |
| 112 | + urlParts.add("weight:" + weight); |
| 113 | + } |
37 | 114 |
|
38 | | - /** |
39 | | - * @param encodedPoints A string representation of a path, encoded with the Polyline Algorithm. |
40 | | - */ |
41 | | - public EncodedPolyline(String encodedPoints) { |
42 | | - this.points = encodedPoints; |
43 | | - } |
| 115 | + if (color != null) { |
| 116 | + urlParts.add("color:" + color); |
| 117 | + } |
44 | 118 |
|
45 | | - /** @param points A path as a collection of {@code LatLng} points. */ |
46 | | - public EncodedPolyline(List<LatLng> points) { |
47 | | - this.points = PolylineEncoding.encode(points); |
48 | | - } |
| 119 | + if (fillcolor != null) { |
| 120 | + urlParts.add("fillcolor:" + fillcolor); |
| 121 | + } |
49 | 122 |
|
50 | | - public String getEncodedPath() { |
51 | | - return points; |
52 | | - } |
| 123 | + if (geodesic) { |
| 124 | + urlParts.add("geodesic:" + geodesic); |
| 125 | + } |
53 | 126 |
|
54 | | - public List<LatLng> decodePath() { |
55 | | - return PolylineEncoding.decode(points); |
56 | | - } |
| 127 | + urlParts.add("enc:" + points); |
57 | 128 |
|
58 | | - // Use the encoded point representation; decoding to get an alternate representation for |
59 | | - // individual points would be expensive. |
60 | | - @Override |
61 | | - public String toString() { |
62 | | - return String.format("[EncodedPolyline: %s]", points); |
63 | | - } |
| 129 | + return StringJoin.join('|', urlParts.toArray(new String[urlParts.size()])); |
| 130 | + } |
64 | 131 | } |
0 commit comments