-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathCustomPolyline.java
More file actions
172 lines (139 loc) · 5.98 KB
/
Copy pathCustomPolyline.java
File metadata and controls
172 lines (139 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.hemangkumar.capacitorgooglemaps;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Consumer;
import com.getcapacitor.JSArray;
import com.getcapacitor.JSObject;
import com.getcapacitor.util.WebColor;
import com.google.android.libraries.maps.GoogleMap;
import com.google.android.libraries.maps.model.LatLng;
import com.google.android.libraries.maps.model.Polyline;
import com.google.android.libraries.maps.model.PolylineOptions;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
public class CustomPolyline {
// generate id for the just added polyline,
// put this polyline into a hashmap with the corresponding id,
// so we can retrieve the polyline by id later on
public final String polylineId = UUID.randomUUID().toString();
private final PolylineOptions polylineOptions = new PolylineOptions();
protected JSObject tag = new JSObject();
public void updateFromJSObject(JSObject polyline) {
this.setPath(polyline.optJSONArray("path"));
final JSObject preferences = JSObjectDefaults.getJSObjectSafe(polyline, "preferences", new JSObject());
this.setBasicFields(preferences);
this.setMetadata(JSObjectDefaults.getJSObjectSafe(preferences, "metadata", new JSObject()));
}
public void addToMap(GoogleMap googleMap, @Nullable Consumer<Polyline> consumer) {
final Polyline polyline = googleMap.addPolyline(polylineOptions);
polyline.setTag(tag);
if (consumer != null) {
consumer.accept(polyline);
}
}
private void setPath(@Nullable JSONArray path) {
if (path != null) {
List<LatLng> latLngList = getLatLngList(path);
this.polylineOptions.addAll(latLngList);
}
}
private static List<LatLng> getLatLngList(@NonNull JSONArray latLngArray) {
List<LatLng> latLngList = new ArrayList<>();
for (int n = 0; n < latLngArray.length(); n++) {
JSONObject latLngObject = latLngArray.optJSONObject(n);
if (latLngObject != null) {
LatLng latLng = getLatLng(latLngObject);
latLngList.add(latLng);
}
}
return latLngList;
}
private static LatLng getLatLng(@NonNull JSONObject latLngObject) {
double latitude = latLngObject.optDouble("latitude", 0d);
double longitude = latLngObject.optDouble("longitude", 0d);
return new LatLng(latitude, longitude);
}
private void setBasicFields(@NonNull JSObject preferences) {
final float width = (float) preferences.optDouble("width", 10);
final int color = WebColor.parseColor(preferences.optString("color", "#000000"));
final float zIndex = (float) preferences.optDouble("zIndex", 0);
final boolean isVisible = preferences.optBoolean("isVisible", true);
final boolean isGeodesic = preferences.optBoolean("isGeodesic", false);
final boolean isClickable = preferences.optBoolean("isClickable", false);
polylineOptions.color(color);
polylineOptions.width(width);
polylineOptions.zIndex(zIndex);
polylineOptions.visible(isVisible);
polylineOptions.geodesic(isGeodesic);
polylineOptions.clickable(isClickable);
}
private void setMetadata(@NonNull JSObject jsObject) {
JSObject tag = new JSObject();
tag.put("id", this.polylineId);
tag.put("metadata", jsObject);
this.tag = tag;
}
public JSObject getResultForPolyline(Polyline polyline, String mapId) {
JSObject tag = null;
try {
tag = (JSObject) polyline.getTag();
} catch (Exception e) {
e.printStackTrace();
} finally {
tag = tag != null ? tag : new JSObject();
}
// initialize JSObjects to return
JSObject result = new JSObject();
JSObject polylineResult = new JSObject();
JSObject preferencesResult = new JSObject();
result.put("polyline", polylineResult);
polylineResult.put("preferences", preferencesResult);
// get map id
polylineResult.put("mapId", mapId);
// get id
String polylineId = tag.optString("polylineId", polyline.getId());
polylineResult.put("polylineId", polylineId);
// get path values
JSArray path = latLngsToJSArray(polyline.getPoints());
polylineResult.put("path", path);
// get preferences
preferencesResult.put("width", polyline.getWidth());
preferencesResult.put("color", colorToString(polyline.getColor()));
preferencesResult.put("zIndex", polyline.getZIndex());
preferencesResult.put("isVisible", polyline.isVisible());
preferencesResult.put("isGeodesic", polyline.isGeodesic());
preferencesResult.put("isClickable", polyline.isClickable());
JSObject metadata = JSObjectDefaults.getJSObjectSafe(tag, "metadata", new JSObject());
preferencesResult.put("metadata", metadata);
return result;
}
private static JSArray latLngsToJSArray(Collection<LatLng> positions) {
JSArray jsPositions = new JSArray();
for (LatLng pos : positions) {
JSObject jsPos = latLngToJSObject(pos);
jsPositions.put(jsPos);
}
return jsPositions;
}
private static JSObject latLngToJSObject(LatLng latLng) {
JSObject jsPos = new JSObject();
jsPos.put("latitude", latLng.latitude);
jsPos.put("longitude", latLng.longitude);
return jsPos;
}
private static String colorToString(int color) {
int r = ((color >> 16) & 0xff);
int g = ((color >> 8) & 0xff);
int b = ((color) & 0xff);
int a = ((color >> 24) & 0xff);
if (a != 255) {
return String.format("#%02X%02X%02X%02X", a, r, g, b);
} else {
return String.format("#%02X%02X%02X", r, g, b);
}
}
}