-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathCustomMapView.java
More file actions
656 lines (539 loc) · 27.1 KB
/
CustomMapView.java
File metadata and controls
656 lines (539 loc) · 27.1 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
package com.hemangkumar.capacitorgooglemaps;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import com.getcapacitor.JSObject;
import com.google.android.libraries.maps.CameraUpdate;
import com.google.android.libraries.maps.CameraUpdateFactory;
import com.google.android.libraries.maps.GoogleMap;
import com.google.android.libraries.maps.GoogleMapOptions;
import com.google.android.libraries.maps.MapView;
import com.google.android.libraries.maps.OnMapReadyCallback;
import com.google.android.libraries.maps.UiSettings;
import com.google.android.libraries.maps.model.CameraPosition;
import com.google.android.libraries.maps.model.LatLng;
import com.google.android.libraries.maps.model.Marker;
import com.google.android.libraries.maps.model.PointOfInterest;
import java.util.HashMap;
import java.util.UUID;
public class CustomMapView
implements OnMapReadyCallback,
GoogleMap.OnInfoWindowClickListener,
GoogleMap.OnInfoWindowCloseListener,
GoogleMap.OnMapClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMarkerDragListener,
GoogleMap.OnMyLocationClickListener,
GoogleMap.OnMyLocationButtonClickListener,
GoogleMap.OnPoiClickListener,
GoogleMap.OnCameraMoveStartedListener,
GoogleMap.OnCameraMoveListener,
GoogleMap.OnCameraIdleListener
{
private final Context context;
private final CustomMapViewEvents customMapViewEvents;
private final String id;
MapView mapView;
GoogleMap googleMap;
private HashMap<String, Marker> markers = new HashMap<>();
String savedCallbackIdForCreate;
String savedCallbackIdForDidTapInfoWindow;
String savedCallbackIdForDidCloseInfoWindow;
String savedCallbackIdForDidTapMap;
String savedCallbackIdForDidLongPressMap;
String savedCallbackIdForDidTapMarker;
Boolean preventDefaultForDidTapMarker = false;
String savedCallbackIdForDidBeginDraggingMarker;
String savedCallbackIdForDidDragMarker;
String savedCallbackIdForDidEndDraggingMarker;
String savedCallbackIdForDidTapMyLocationButton;
Boolean preventDefaultForDidTapMyLocationButton = false;
String savedCallbackIdForDidTapMyLocationDot;
String savedCallbackIdForDidTapPoi;
String savedCallbackIdForDidBeginMovingCamera;
String savedCallbackIdForDidMoveCamera;
String savedCallbackIdForDidEndMovingCamera;
public static final String EVENT_DID_TAP_INFO_WINDOW = "didTapInfoWindow";
public static final String EVENT_DID_CLOSE_INFO_WINDOW = "didCloseInfoWindow";
public static final String EVENT_DID_TAP_MAP = "didTapMap";
public static final String EVENT_DID_LONG_PRESS_MAP = "didLongPressMap";
public static final String EVENT_DID_TAP_MARKER = "didTapMarker";
public static final String EVENT_DID_BEGIN_DRAGGING_MARKER = "didBeginDraggingMarker";
public static final String EVENT_DID_DRAG_MARKER = "didDragMarker";
public static final String EVENT_DID_END_DRAGGING_MARKER = "didEndDraggingMarker";
public static final String EVENT_DID_TAP_MY_LOCATION_BUTTON = "didTapMyLocationButton";
public static final String EVENT_DID_TAP_MY_LOCATION_DOT = "didTapMyLocationDot";
public static final String EVENT_DID_TAP_POI = "didTapPoi";
public static final String EVENT_DID_BEGIN_MOVING_CAMERA = "didBeginMovingCamera";
public static final String EVENT_DID_MOVE_CAMERA = "didMoveCamera";
public static final String EVENT_DID_END_MOVING_CAMERA = "didEndMovingCamera";
public MapCameraPosition mapCameraPosition;
public MapPreferences mapPreferences;
public CustomMapView(@NonNull Context context, CustomMapViewEvents customMapViewEvents) {
this.context = context;
this.customMapViewEvents = customMapViewEvents;
this.id = UUID.randomUUID().toString();
}
public String getId() {
return id;
}
private boolean hasPermission() {
return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
@SuppressLint("MissingPermission")
@Override
public void onMapReady(GoogleMap googleMap) {
// populate `googleMap` variable for other methods to use
this.googleMap = googleMap;
// set controls
UiSettings googleMapUISettings = this.googleMap.getUiSettings();
googleMapUISettings.setIndoorLevelPickerEnabled(this.mapPreferences.controls.getBoolean(MapPreferencesControls.INDOOR_LEVEL_PICKER_KEY));
googleMapUISettings.setMyLocationButtonEnabled(this.mapPreferences.controls.getBoolean(MapPreferencesControls.MY_LOCATION_BUTTON_KEY));
// set appearance
this.googleMap.setMapStyle(this.mapPreferences.appearance.style);
this.googleMap.setBuildingsEnabled(this.mapPreferences.appearance.isBuildingsShown);
this.googleMap.setIndoorEnabled(this.mapPreferences.appearance.isIndoorShown);
if (hasPermission()) {
this.googleMap.setMyLocationEnabled(this.mapPreferences.appearance.isMyLocationDotShown);
}
this.googleMap.setTrafficEnabled(this.mapPreferences.appearance.isTrafficShown);
// execute callback
if (customMapViewEvents != null && savedCallbackIdForCreate != null) {
JSObject result = getResultForMap();
customMapViewEvents.onMapReady(savedCallbackIdForCreate, result);
}
}
@Override
public void onInfoWindowClick(Marker marker) {
if (customMapViewEvents != null && savedCallbackIdForDidTapInfoWindow != null) {
JSObject result = CustomMarker.getResultForMarker(marker, this.id);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidTapInfoWindow, result);
}
}
@Override
public void onInfoWindowClose(Marker marker) {
if (customMapViewEvents != null && savedCallbackIdForDidCloseInfoWindow != null) {
JSObject result = CustomMarker.getResultForMarker(marker, this.id);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidCloseInfoWindow, result);
}
}
@Override
public void onMapClick(LatLng latLng) {
if (customMapViewEvents != null && savedCallbackIdForDidTapMap != null) {
JSObject result = getResultForPosition(latLng);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidTapMap, result);
}
}
@Override
public void onMapLongClick(LatLng latLng) {
if (customMapViewEvents != null && savedCallbackIdForDidLongPressMap != null) {
JSObject result = getResultForPosition(latLng);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidLongPressMap, result);
}
}
@Override
public boolean onMarkerClick(Marker marker) {
if (customMapViewEvents != null && savedCallbackIdForDidTapMarker != null) {
JSObject result = CustomMarker.getResultForMarker(marker, this.id);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidTapMarker, result);
}
return preventDefaultForDidTapMarker;
}
@Override
public void onMarkerDragStart(Marker marker) {
if (customMapViewEvents != null && savedCallbackIdForDidBeginDraggingMarker != null) {
JSObject result = CustomMarker.getResultForMarker(marker, this.id);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidBeginDraggingMarker, result);
}
}
@Override
public void onMarkerDrag(Marker marker) {
if (customMapViewEvents != null && savedCallbackIdForDidDragMarker != null) {
JSObject result = CustomMarker.getResultForMarker(marker, this.id);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidDragMarker, result);
}
}
@Override
public void onMarkerDragEnd(Marker marker) {
if (customMapViewEvents != null && savedCallbackIdForDidEndDraggingMarker != null) {
JSObject result = CustomMarker.getResultForMarker(marker, this.id);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidEndDraggingMarker, result);
}
}
@Override
public boolean onMyLocationButtonClick() {
if (customMapViewEvents != null && savedCallbackIdForDidTapMyLocationButton != null) {
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidTapMyLocationButton, null);
}
return preventDefaultForDidTapMyLocationButton;
}
@Override
public void onMyLocationClick(@NonNull Location location) {
if (customMapViewEvents != null && savedCallbackIdForDidTapMyLocationDot != null) {
JSObject result = getResultForPosition(location);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidTapMyLocationDot, result);
}
}
@Override
public void onPoiClick(PointOfInterest pointOfInterest) {
if (customMapViewEvents != null && savedCallbackIdForDidTapPoi != null) {
JSObject result = getResultForPoi(pointOfInterest);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidTapPoi, result);
}
}
@Override
public void onCameraMoveStarted(int i) {
if (customMapViewEvents != null && savedCallbackIdForDidBeginMovingCamera != null) {
int reason = 2;
if (i == REASON_GESTURE) {
// Camera motion initiated in response to user gestures on the map.
// For example: pan, tilt, pinch to zoom, or rotate.
reason = 1;
}
JSObject result = new JSObject();
result.put("reason", reason);
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidBeginMovingCamera, result);
}
}
@Override
public void onCameraMove() {
if (customMapViewEvents != null && savedCallbackIdForDidMoveCamera != null) {
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidMoveCamera, null);
}
}
@Override
public void onCameraIdle() {
if (customMapViewEvents != null && savedCallbackIdForDidEndMovingCamera != null) {
customMapViewEvents.resultForCallbackId(savedCallbackIdForDidEndMovingCamera, this.getResultForCameraPosition(new JSObject()));
}
}
protected void handleOnStart() {
if (mapView != null) {
mapView.onStart();
}
}
protected void handleOnResume() {
if (mapView != null) {
mapView.onResume();
}
}
protected void handleOnPause() {
if (mapView != null) {
mapView.onPause();
}
}
protected void handleOnStop() {
if (mapView != null) {
mapView.onStop();
}
}
protected void handleOnDestroy() {
if (mapView != null) {
mapView.onDestroy();
}
}
public void setCallbackIdForEvent(String callbackId, String eventName, Boolean preventDefault) {
if (callbackId != null && eventName != null) {
if (eventName.equals(CustomMapView.EVENT_DID_TAP_INFO_WINDOW)) {
this.googleMap.setOnInfoWindowClickListener(this);
savedCallbackIdForDidTapInfoWindow = callbackId;
} else if (eventName.equals(CustomMapView.EVENT_DID_CLOSE_INFO_WINDOW)) {
this.googleMap.setOnInfoWindowCloseListener(this);
savedCallbackIdForDidCloseInfoWindow = callbackId;
} else if (eventName.equals(CustomMapView.EVENT_DID_TAP_MAP)) {
this.googleMap.setOnMapClickListener(this);
savedCallbackIdForDidTapMap = callbackId;
} else if (eventName.equals(CustomMapView.EVENT_DID_LONG_PRESS_MAP)) {
this.googleMap.setOnMapLongClickListener(this);
savedCallbackIdForDidLongPressMap = callbackId;
} else if (eventName.equals(CustomMapView.EVENT_DID_TAP_MARKER)) {
this.googleMap.setOnMarkerClickListener(this);
savedCallbackIdForDidTapMarker = callbackId;
if (preventDefault == null) {
preventDefault = false;
}
preventDefaultForDidTapMarker = preventDefault;
} else if (eventName.equals(CustomMapView.EVENT_DID_BEGIN_DRAGGING_MARKER)) {
this.googleMap.setOnMarkerDragListener(this);
savedCallbackIdForDidBeginDraggingMarker = callbackId;
} else if (eventName.equals(CustomMapView.EVENT_DID_DRAG_MARKER)) {
this.googleMap.setOnMarkerDragListener(this);
savedCallbackIdForDidDragMarker = callbackId;
} else if (eventName.equals(CustomMapView.EVENT_DID_END_DRAGGING_MARKER)) {
this.googleMap.setOnMarkerDragListener(this);
savedCallbackIdForDidEndDraggingMarker = callbackId;
} else if (eventName.equals(CustomMapView.EVENT_DID_TAP_MY_LOCATION_BUTTON)) {
this.googleMap.setOnMyLocationButtonClickListener(this);
savedCallbackIdForDidTapMyLocationButton = callbackId;
if (preventDefault == null) {
preventDefault = false;
}
preventDefaultForDidTapMyLocationButton = preventDefault;
} else if (eventName.equals(CustomMapView.EVENT_DID_TAP_MY_LOCATION_DOT)) {
this.googleMap.setOnMyLocationClickListener(this);
savedCallbackIdForDidTapMyLocationDot = callbackId;
} else if (eventName.equals((CustomMapView.EVENT_DID_TAP_POI))) {
this.googleMap.setOnPoiClickListener(this);
savedCallbackIdForDidTapPoi = callbackId;
} else if (eventName.equals((CustomMapView.EVENT_DID_BEGIN_MOVING_CAMERA))) {
this.googleMap.setOnCameraMoveStartedListener(this);
savedCallbackIdForDidBeginMovingCamera = callbackId;
} else if (eventName.equals((CustomMapView.EVENT_DID_MOVE_CAMERA))) {
this.googleMap.setOnCameraMoveListener(this);
savedCallbackIdForDidMoveCamera = callbackId;
} else if (eventName.equals((CustomMapView.EVENT_DID_END_MOVING_CAMERA))) {
this.googleMap.setOnCameraIdleListener(this);
savedCallbackIdForDidEndMovingCamera = callbackId;
}
}
}
public void createMap(String callbackId, BoundingRect boundingRect, MapCameraPosition mapCameraPosition, MapPreferences mapPreferences) {
savedCallbackIdForCreate = callbackId;
this.mapCameraPosition = mapCameraPosition;
this.mapPreferences = mapPreferences;
GoogleMapOptions googleMapOptions = this.mapPreferences.generateGoogleMapOptions();
googleMapOptions.camera(this.mapCameraPosition.cameraPosition);
mapView = new MapView(context, googleMapOptions);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(getScaledPixels(boundingRect.width), getScaledPixels(boundingRect.height));
lp.topMargin = getScaledPixels(boundingRect.y);
lp.leftMargin = getScaledPixels(boundingRect.x);
mapView.setLayoutParams(lp);
mapView.onCreate(null);
mapView.onStart();
mapView.getMapAsync(this);
}
@SuppressLint("MissingPermission")
public JSObject invalidateMap() {
if (this.googleMap == null) {
return null;
}
UiSettings googleMapUISettings = this.googleMap.getUiSettings();
// set gestures
googleMapUISettings.setRotateGesturesEnabled(this.mapPreferences.gestures.getBoolean(MapPreferencesGestures.ROTATE_ALLOWED_KEY));
googleMapUISettings.setScrollGesturesEnabled(this.mapPreferences.gestures.getBoolean(MapPreferencesGestures.SCROLL_ALLOWED_KEY));
googleMapUISettings.setScrollGesturesEnabledDuringRotateOrZoom(this.mapPreferences.gestures.getBoolean(MapPreferencesGestures.SCROLL_ALLOWED_DURING_ROTATE_OR_ZOOM_KEY));
googleMapUISettings.setTiltGesturesEnabled(this.mapPreferences.gestures.getBoolean(MapPreferencesGestures.TILT_ALLOWED_KEY));
googleMapUISettings.setZoomGesturesEnabled(this.mapPreferences.gestures.getBoolean(MapPreferencesGestures.ZOOM_ALLOWED_KEY));
// set controls
googleMapUISettings.setCompassEnabled(this.mapPreferences.controls.getBoolean(MapPreferencesControls.COMPASS_BUTTON_KEY));
googleMapUISettings.setIndoorLevelPickerEnabled(this.mapPreferences.controls.getBoolean(MapPreferencesControls.INDOOR_LEVEL_PICKER_KEY));
googleMapUISettings.setMapToolbarEnabled(this.mapPreferences.controls.getBoolean(MapPreferencesControls.MAP_TOOLBAR_KEY));
googleMapUISettings.setMyLocationButtonEnabled(this.mapPreferences.controls.getBoolean(MapPreferencesControls.MY_LOCATION_BUTTON_KEY));
googleMapUISettings.setZoomControlsEnabled(this.mapPreferences.controls.getBoolean(MapPreferencesControls.ZOOM_BUTTONS_KEY));
// set appearance
this.googleMap.setMapType(this.mapPreferences.appearance.type);
this.googleMap.setMapStyle(this.mapPreferences.appearance.style);
this.googleMap.setBuildingsEnabled(this.mapPreferences.appearance.isBuildingsShown);
this.googleMap.setIndoorEnabled(this.mapPreferences.appearance.isIndoorShown);
if (hasPermission()) {
this.googleMap.setMyLocationEnabled(this.mapPreferences.appearance.isMyLocationDotShown);
}
this.googleMap.setTrafficEnabled(this.mapPreferences.appearance.isTrafficShown);
return getResultForMap();
}
public JSObject getMap() {
return getResultForMap();
}
public CameraPosition getCameraPosition() {
if (this.googleMap != null) {
return this.googleMap.getCameraPosition();
}
return null;
}
public void moveCamera(Integer duration) {
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(this.mapCameraPosition.cameraPosition);
if (duration == null || duration <= 0) {
googleMap.moveCamera(cameraUpdate);
} else {
googleMap.animateCamera(cameraUpdate, duration, null);
}
}
private int getScaledPixels(float pixels) {
// Get the screen's density scale
final float scale = context.getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
public void addToView(ViewGroup parent) {
parent.addView(mapView);
}
public void removeFromView(ViewGroup parent) {
parent.removeView(mapView);
}
public void clear() {
googleMap.clear();
markers.clear();
}
public Marker addMarker(CustomMarker customMarker) {
Marker marker = customMarker.addToMap(googleMap);
markers.put(customMarker.markerId, marker);
return marker;
}
public void removeMarker(String markerId) {
Marker marker = markers.get(markerId);
if (marker != null) {
marker.remove();
markers.remove(markerId);
}
}
private JSObject getResultForMap() {
if (this.mapView != null && this.googleMap != null) {
// initialize JSObjects
JSObject result = new JSObject();
JSObject resultGoogleMap = new JSObject();
result.put("googleMap", resultGoogleMap);
JSObject resultPreferences = new JSObject();
resultGoogleMap.put("preferences", resultPreferences);
JSObject resultGestures = new JSObject();
resultPreferences.put("gestures", resultGestures);
JSObject resultControls = new JSObject();
resultPreferences.put("controls", resultControls);
JSObject resultAppearance = new JSObject();
resultPreferences.put("appearance", resultAppearance);
// get UISettings
UiSettings googleMapUISettings = this.googleMap.getUiSettings();
// return mapId
resultGoogleMap.put("mapId", id);
// return cameraPosition
this.getResultForCameraPosition(resultGoogleMap);
// return gestures
resultGestures.put(MapPreferencesGestures.ROTATE_ALLOWED_KEY, googleMapUISettings.isRotateGesturesEnabled());
resultGestures.put(MapPreferencesGestures.SCROLL_ALLOWED_KEY, googleMapUISettings.isScrollGesturesEnabled());
resultGestures.put(MapPreferencesGestures.SCROLL_ALLOWED_DURING_ROTATE_OR_ZOOM_KEY, googleMapUISettings.isScrollGesturesEnabledDuringRotateOrZoom());
resultGestures.put(MapPreferencesGestures.TILT_ALLOWED_KEY, googleMapUISettings.isTiltGesturesEnabled());
resultGestures.put(MapPreferencesGestures.ZOOM_ALLOWED_KEY, googleMapUISettings.isZoomGesturesEnabled());
// return controls
resultControls.put(MapPreferencesControls.COMPASS_BUTTON_KEY, googleMapUISettings.isCompassEnabled());
// resultControls.put(MapPreferencesControls.INDOOR_LEVEL_PICKER_KEY, googleMapUISettings.isIndoorLevelPickerEnabled());
resultControls.put(MapPreferencesControls.MAP_TOOLBAR_KEY, googleMapUISettings.isMapToolbarEnabled());
resultControls.put(MapPreferencesControls.MY_LOCATION_BUTTON_KEY, googleMapUISettings.isMyLocationButtonEnabled());
resultControls.put(MapPreferencesControls.ZOOM_BUTTONS_KEY, googleMapUISettings.isZoomControlsEnabled());
// return appearance
resultAppearance.put(MapPreferencesAppearance.TYPE_KEY, this.googleMap.getMapType());
resultAppearance.put(MapPreferencesAppearance.BUILDINGS_SHOWN_KEY, this.googleMap.isBuildingsEnabled());
resultAppearance.put(MapPreferencesAppearance.INDOOR_SHOWN_KEY, this.googleMap.isIndoorEnabled());
resultAppearance.put(MapPreferencesAppearance.MY_LOCATION_DOT_SHOWN_KEY, this.googleMap.isMyLocationEnabled());
resultAppearance.put(MapPreferencesAppearance.TRAFFIC_SHOWN_KEY, this.googleMap.isTrafficEnabled());
return result;
}
return null;
}
private JSObject getResultForCameraPosition(JSObject resultObjectToExtend) {
JSObject resultCameraPosition = new JSObject();
resultObjectToExtend.put("cameraPosition", resultCameraPosition);
JSObject resultCameraPositionTarget = new JSObject();
resultCameraPosition.put("target", resultCameraPositionTarget);
// get CameraPosition
CameraPosition cameraPosition = this.getCameraPosition();
// return cameraPosition
resultCameraPositionTarget.put("latitude", cameraPosition.target.latitude);
resultCameraPositionTarget.put("longitude", cameraPosition.target.longitude);
resultCameraPosition.put("bearing", cameraPosition.bearing);
resultCameraPosition.put("tilt", cameraPosition.tilt);
resultCameraPosition.put("zoom", cameraPosition.zoom);
return resultObjectToExtend;
}
private JSObject getResultForPosition(Location location) {
// initialize JSObjects to return
JSObject result = new JSObject();
JSObject positionResult = new JSObject();
result.put("position", positionResult);
// get position values
positionResult.put("latitude", location.getLatitude());
positionResult.put("longitude", location.getLongitude());
// return result
return result;
}
private JSObject getResultForPosition(LatLng latLng) {
// initialize JSObjects to return
JSObject result = new JSObject();
JSObject positionResult = new JSObject();
result.put("position", positionResult);
// get position values
positionResult.put("latitude", latLng.latitude);
positionResult.put("longitude", latLng.longitude);
// return result
return result;
}
private JSObject getResultForPoi(PointOfInterest pointOfInterest) {
// initialize JSObjects to return
JSObject result = new JSObject();
JSObject poiResult = new JSObject();
JSObject positionResult = new JSObject();
result.put("poi", poiResult);
poiResult.put("position", positionResult);
// get position values
positionResult.put("latitude", pointOfInterest.latLng.latitude);
positionResult.put("longitude", pointOfInterest.latLng.longitude);
// get other values
poiResult.put("name", pointOfInterest.name);
poiResult.put("placeId", pointOfInterest.placeId);
// return result
return result;
}
public void addPolyline(final PluginCall call) {
final JSArray points = call.getArray("points", new JSArray());
getBridge().executeOnMainThread(new Runnable() {
@Override
public void run() {
PolylineOptions polylineOptions = new PolylineOptions();
for (int i = 0; i < points.length(); i++) {
try {
JSONObject point = points.getJSONObject(i);
LatLng latLng = new LatLng(point.getDouble("latitude"), point.getDouble("longitude"));
polylineOptions.add(latLng);
} catch (JSONException e) {
e.printStackTrace();
}
}
googleMap.addPolyline(polylineOptions);
call.resolve();
}
});
}
public void addPolygon(final PluginCall call) {
final JSArray points = call.getArray("points", new JSArray());
getBridge().executeOnMainThread(new Runnable() {
@Override
public void run() {
PolygonOptions polygonOptions = new PolygonOptions();
for (int i = 0; i < points.length(); i++) {
try {
JSONObject point = points.getJSONObject(i);
LatLng latLng = new LatLng(point.getDouble("latitude"), point.getDouble("longitude"));
polygonOptions.add(latLng);
} catch (JSONException e) {
e.printStackTrace();
}
}
googleMap.addPolygon(polygonOptions);
call.resolve();
}
});
}
public void addCircle(final PluginCall call) {
final int radius = call.getInt("radius", 0);
final JSONObject center = call.getObject("center", new JSObject());
getBridge().executeOnMainThread(new Runnable() {
@Override
public void run() {
CircleOptions circleOptions = new CircleOptions();
circleOptions.radius(radius);
try {
circleOptions.center(new LatLng(center.getDouble("latitude"), center.getDouble("longitude")));
} catch (JSONException e) {
e.printStackTrace();
}
googleMap.addCircle(circleOptions);
call.resolve();
}
});
}
}