-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathRenderer.java
More file actions
1245 lines (1137 loc) · 48.1 KB
/
Renderer.java
File metadata and controls
1245 lines (1137 loc) · 48.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.maps.android.data;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlay;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.maps.android.R;
import com.google.maps.android.collections.GroundOverlayManager;
import com.google.maps.android.collections.MarkerManager;
import com.google.maps.android.collections.PolygonManager;
import com.google.maps.android.collections.PolylineManager;
import com.google.maps.android.data.geojson.BiMultiMap;
import com.google.maps.android.data.geojson.GeoJsonFeature;
import com.google.maps.android.data.geojson.GeoJsonGeometryCollection;
import com.google.maps.android.data.geojson.GeoJsonLineString;
import com.google.maps.android.data.geojson.GeoJsonLineStringStyle;
import com.google.maps.android.data.geojson.GeoJsonMultiLineString;
import com.google.maps.android.data.geojson.GeoJsonMultiPoint;
import com.google.maps.android.data.geojson.GeoJsonMultiPolygon;
import com.google.maps.android.data.geojson.GeoJsonPoint;
import com.google.maps.android.data.geojson.GeoJsonPointStyle;
import com.google.maps.android.data.geojson.GeoJsonPolygon;
import com.google.maps.android.data.geojson.GeoJsonPolygonStyle;
import com.google.maps.android.data.kml.KmlContainer;
import com.google.maps.android.data.kml.KmlGroundOverlay;
import com.google.maps.android.data.kml.KmlMultiGeometry;
import com.google.maps.android.data.kml.KmlPlacemark;
import com.google.maps.android.data.kml.KmlPoint;
import com.google.maps.android.data.kml.KmlStyle;
import com.google.maps.android.data.kml.KmlUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* An abstraction that shares the common properties of
* {@link com.google.maps.android.data.kml.KmlRenderer KmlRenderer} and
* {@link com.google.maps.android.data.geojson.GeoJsonRenderer GeoJsonRenderer}
*/
public class Renderer {
private static final int MARKER_ICON_SIZE = 32;
private static final Object FEATURE_NOT_ON_MAP = null;
private static final DecimalFormat sScaleFormat = new DecimalFormat("#.####");
private GoogleMap mMap;
private final BiMultiMap<Feature> mFeatures = new BiMultiMap<>();
private HashMap<String, KmlStyle> mStyles;
private HashMap<String, KmlStyle> mStylesRenderer;
private HashMap<String, String> mStyleMaps;
private final BiMultiMap<Feature> mContainerFeatures;
private HashMap<KmlGroundOverlay, GroundOverlay> mGroundOverlayMap;
private final Set<String> mMarkerIconUrls;
private ImagesCache mImagesCache;
private int mNumActiveDownloads = 0;
private boolean mLayerOnMap;
private Context mContext;
private ArrayList<KmlContainer> mContainers;
private final GeoJsonPointStyle mDefaultPointStyle;
private final GeoJsonLineStringStyle mDefaultLineStringStyle;
private final GeoJsonPolygonStyle mDefaultPolygonStyle;
private final MarkerManager.Collection mMarkers;
private final PolygonManager.Collection mPolygons;
private final PolylineManager.Collection mPolylines;
private final GroundOverlayManager.Collection mGroundOverlays;
/**
* Creates a new Renderer object for KML features
*
* @param map map to place objects on
* @param context the Context
* @param markerManager marker manager to create marker collection from
* @param polygonManager polygon manager to create polygon collection from
* @param polylineManager polyline manager to create polyline collection from
* @param groundOverlayManager ground overlay manager to create ground overlay collection from
* @param imagesCache an optional ImagesCache to be used for caching images fetched
*/
public Renderer(GoogleMap map,
Context context,
MarkerManager markerManager,
PolygonManager polygonManager,
PolylineManager polylineManager,
GroundOverlayManager groundOverlayManager,
@Nullable ImagesCache imagesCache) {
this(map, new HashSet<String>(), null, null, null, new BiMultiMap<Feature>(), markerManager, polygonManager, polylineManager, groundOverlayManager);
mContext = context;
mStylesRenderer = new HashMap<>();
mImagesCache = (imagesCache == null) ? new ImagesCache() : imagesCache;
}
/**
* Creates a new Renderer object for GeoJSON features
*
* @param map map to place objects on
* @param features contains a hashmap of features and objects that will go on the map
* @param markerManager marker manager to create marker collection from
* @param polygonManager polygon manager to create polygon collection from
* @param polylineManager polyline manager to create polyline collection from
* @param groundOverlayManager ground overlay manager to create ground overlay collection from
*/
public Renderer(GoogleMap map, HashMap<? extends Feature, Object> features, MarkerManager markerManager, PolygonManager polygonManager, PolylineManager polylineManager, GroundOverlayManager groundOverlayManager) {
this(map, null, new GeoJsonPointStyle(), new GeoJsonLineStringStyle(), new GeoJsonPolygonStyle(), null, markerManager, polygonManager, polylineManager, groundOverlayManager);
mFeatures.putAll(features);
mImagesCache = null;
}
private Renderer(GoogleMap map,
Set<String> markerIconUrls,
GeoJsonPointStyle defaultPointStyle,
GeoJsonLineStringStyle defaultLineStringStyle,
GeoJsonPolygonStyle defaultPolygonStyle,
BiMultiMap<Feature> containerFeatures,
MarkerManager markerManager,
PolygonManager polygonManager,
PolylineManager polylineManager,
GroundOverlayManager groundOverlayManager) {
mMap = map;
mLayerOnMap = false;
mMarkerIconUrls = markerIconUrls;
mDefaultPointStyle = defaultPointStyle;
mDefaultLineStringStyle = defaultLineStringStyle;
mDefaultPolygonStyle = defaultPolygonStyle;
mContainerFeatures = containerFeatures;
if (map != null) {
if (markerManager == null) {
markerManager = new MarkerManager(map);
}
mMarkers = markerManager.newCollection();
if (polygonManager == null) {
polygonManager = new PolygonManager(map);
}
mPolygons = polygonManager.newCollection();
if (polylineManager == null) {
polylineManager = new PolylineManager(map);
}
mPolylines = polylineManager.newCollection();
if (groundOverlayManager == null) {
groundOverlayManager = new GroundOverlayManager(map);
}
mGroundOverlays = groundOverlayManager.newCollection();
} else {
mMarkers = null;
mPolygons = null;
mPolylines = null;
mGroundOverlays = null;
}
}
public static final class ImagesCache {
/**
* Map of image URL to map of scale factor to BitmapDescriptors for point marker icons
*
* BitmapDescriptors are cached to avoid creating new BitmapDescriptors for each individual
* usage of a Bitmap. Each BitmapDescriptor copies the Bitmap it's created from.
*/
final Map<String, Map<String, BitmapDescriptor>> markerImagesCache = new HashMap<>();
/**
* Map of image URL to BitmapDescriptors for non-scaled ground overlay images
*/
final Map<String, BitmapDescriptor> groundOverlayImagesCache = new HashMap<>();
/**
* Map of image URL to Bitmap
*
* Holds initial references to bitmaps so they can be scaled and BitmapDescriptors cached.
* This cache is cleared once all icon URLs are loaded, scaled, and cached as BitmapDescriptors.
*/
final Map<String, Bitmap> bitmapCache = new HashMap<>();
}
/**
* Checks if layer has been added to map
*
* @return true if layer is on map, false otherwise
*/
public boolean isLayerOnMap() {
return mLayerOnMap;
}
/**
* Sets the visibility of the layer
*
* @param layerOnMap contains true if the layer should be set to visible and false otherwise
*/
protected void setLayerVisibility(boolean layerOnMap) {
mLayerOnMap = layerOnMap;
}
/**
* Gets the GoogleMap that Feature objects are being placed on
*
* @return GoogleMap
*/
public GoogleMap getMap() {
return mMap;
}
/**
* Sets the map that objects are being placed on
*
* @param map map to place all objects on
*/
public void setMap(GoogleMap map) {
mMap = map;
}
protected void putContainerFeature(Object mapObject, Feature placemark) {
mContainerFeatures.put(placemark, mapObject);
}
/**
* Gets a set containing Features
*
* @return set containing Features
*/
public Set<Feature> getFeatures() {
return mFeatures.keySet();
}
/**
* Gets a Feature for the given map object, which is a Marker, Polyline or Polygon.
*
* @param mapObject Marker, Polyline or Polygon
* @return Feature for the given map object
*/
Feature getFeature(Object mapObject) {
return mFeatures.getKey(mapObject);
}
Feature getContainerFeature(Object mapObject) {
if (mContainerFeatures != null) {
return mContainerFeatures.getKey(mapObject);
}
return null;
}
/**
* getValues is called to retrieve the values stored in the mFeatures
* hashmap.
*
* @return mFeatures.values() collection of values stored in mFeatures
*/
public Collection<Object> getValues() {
return mFeatures.values();
}
/**
* Gets a hashmap of all the features and objects that are on this layer
*
* @return mFeatures hashmap
*/
protected HashMap<? extends Feature, Object> getAllFeatures() {
return mFeatures;
}
/**
* Gets the URLs stored for the Marker icons
*
* @return mMarkerIconUrls Set of URLs
*/
protected Set<String> getMarkerIconUrls() {
return mMarkerIconUrls;
}
/**
* Gets the styles for KML placemarks
*
* @return mStylesRenderer hashmap containing styles for KML placemarks (String, KmlStyle)
*/
protected HashMap<String, KmlStyle> getStylesRenderer() {
return mStylesRenderer;
}
/**
* Gets the styles for KML placemarks
*
* @return mStyleMaps hashmap containing styles for KML placemarks (String, String)
*/
protected HashMap<String, String> getStyleMaps() {
return mStyleMaps;
}
/**
* Gets a cached image at the specified scale which is needed for Marker icon images.
* If a BitmapDescriptor doesn't exist in the cache, the Bitmap for the URL from the
* bitmap cache is scaled and cached as a BitmapDescriptor.
*
* @param url URL to get cached image for
* @param scale scale to get image at
* @return scaled BitmapDescriptor
*/
protected BitmapDescriptor getCachedMarkerImage(String url, double scale) {
String scaleString = sScaleFormat.format(scale);
Map<String, BitmapDescriptor> bitmaps = mImagesCache.markerImagesCache.get(url);
BitmapDescriptor bitmapDescriptor = null;
if (bitmaps != null) {
bitmapDescriptor = bitmaps.get(scaleString);
}
if (bitmapDescriptor == null) {
Bitmap bitmap = mImagesCache.bitmapCache.get(url);
if (bitmap != null) {
bitmapDescriptor = scaleIcon(bitmap, scale);
putMarkerImagesCache(url, scaleString, bitmapDescriptor);
}
}
return bitmapDescriptor;
}
/**
* Scales a bitmap by a specified float, taking into account the display density such
* that the bitmap is scaled for a standard sized KML point marker.
*
* @param unscaledBitmap Unscaled bitmap image to scale.
* @param scale Scale value. A "1.0" scale value corresponds to the original size of the Bitmap
* @return A scaled bitmap image
*/
private BitmapDescriptor scaleIcon(Bitmap unscaledBitmap, double scale) {
float density = mContext.getResources().getDisplayMetrics().density;
int minSize = (int) (MARKER_ICON_SIZE * density * scale);
int unscaledWidth = unscaledBitmap.getWidth();
int unscaledHeight = unscaledBitmap.getHeight();
int width;
int height;
if (unscaledWidth < unscaledHeight) {
width = minSize;
height = (int) ((float) (minSize * unscaledHeight) / (float) unscaledWidth);
} else if (unscaledWidth > unscaledHeight) {
width = (int) ((float) (minSize * unscaledWidth) / (float) unscaledHeight);
height = minSize;
} else {
width = minSize;
height = minSize;
}
Bitmap scaledBitmap = Bitmap.createScaledBitmap(unscaledBitmap, width, height, false);
return BitmapDescriptorFactory.fromBitmap(scaledBitmap);
}
/**
* Gets a cached image needed for GroundOverlays images
*
* @param url URL to get cached image for
* @return BitmapDescriptor
*/
protected BitmapDescriptor getCachedGroundOverlayImage(String url) {
BitmapDescriptor bitmapDescriptor = mImagesCache.groundOverlayImagesCache.get(url);
if (bitmapDescriptor == null) {
Bitmap bitmap = mImagesCache.bitmapCache.get(url);
if (bitmap != null) {
bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap);
mImagesCache.groundOverlayImagesCache.put(url, bitmapDescriptor);
}
}
return bitmapDescriptor;
}
/**
* Gets the ground overlays on the current layer
*
* @return mGroundOverlayMap hashmap contains the ground overlays
*/
public HashMap<KmlGroundOverlay, GroundOverlay> getGroundOverlayMap() {
return mGroundOverlayMap;
}
/**
* Gets the list of KmlContainers that are on the current layer
*
* @return mContainers list of KmlContainers
*/
protected ArrayList<KmlContainer> getContainerList() {
return mContainers;
}
/**
* Obtains the styleUrl from a placemark and finds the corresponding style in a list
*
* @param styleId StyleUrl from a placemark
* @return Style which corresponds to an ID
*/
protected KmlStyle getPlacemarkStyle(String styleId) {
KmlStyle style = mStylesRenderer.get(null);
if (mStylesRenderer.get(styleId) != null) {
style = mStylesRenderer.get(styleId);
}
return style;
}
/**
* Gets the default style used to render GeoJsonPoints
*
* @return default style used to render GeoJsonPoints
*/
GeoJsonPointStyle getDefaultPointStyle() {
return mDefaultPointStyle;
}
/**
* Gets the default style used to render GeoJsonLineStrings
*
* @return default style used to render GeoJsonLineStrings
*/
GeoJsonLineStringStyle getDefaultLineStringStyle() {
return mDefaultLineStringStyle;
}
/**
* Gets the default style used to render GeoJsonPolygons
*
* @return default style used to render GeoJsonPolygons
*/
GeoJsonPolygonStyle getDefaultPolygonStyle() {
return mDefaultPolygonStyle;
}
/**
* Adds a new mapping to the mFeatures hashmap
*
* @param feature Feature to be added onto the map
* @param object Corresponding map object to this feature
*/
protected void putFeatures(Feature feature, Object object) {
mFeatures.put(feature, object);
}
/**
* Adds mStyles to the mStylesRenderer
*/
protected void putStyles() {
mStylesRenderer.putAll(mStyles);
}
/**
* Stores new mappings into the mStylesRenderer hashmap
*
* @param styles hashmap of strings and KmlStyles to be added to mStylesRenderer
*/
protected void putStyles(HashMap<String, KmlStyle> styles) {
mStylesRenderer.putAll(styles);
}
/**
* Cache the scaled BitmapDescriptor for the URL
*
* @param url URL image was loaded from
* @param scale scale the image was scaled to as a formatted string for the cache
* @param bitmapDescriptor BitmapDescriptor to cache for reuse
*/
private void putMarkerImagesCache(String url, String scale, BitmapDescriptor bitmapDescriptor) {
Map<String, BitmapDescriptor> bitmaps = mImagesCache.markerImagesCache.get(url);
if (bitmaps == null) {
bitmaps = new HashMap<>();
mImagesCache.markerImagesCache.put(url, bitmaps);
}
bitmaps.put(scale, bitmapDescriptor);
}
/**
* Cache loaded bitmap images
*
* @param url image URL
* @param bitmap image bitmap
*/
protected void cacheBitmap(String url, Bitmap bitmap) {
mImagesCache.bitmapCache.put(url, bitmap);
}
/**
* Increment active download count
*/
protected void downloadStarted() {
mNumActiveDownloads++;
}
/**
* Decrement active download count and check if bitmap cache should be cleared
*/
protected void downloadFinished() {
mNumActiveDownloads--;
checkClearBitmapCache();
}
/**
* Clear bitmap cache if no active image downloads remain. All images
* should be loaded, scaled, and cached as BitmapDescriptors at this point.
*/
protected void checkClearBitmapCache() {
if (mNumActiveDownloads == 0 && mImagesCache != null && !mImagesCache.bitmapCache.isEmpty()) {
mImagesCache.bitmapCache.clear();
}
}
/**
* Checks if the layer contains placemarks
*
* @return true if there are placemarks, false otherwise
*/
protected boolean hasFeatures() {
return mFeatures.size() > 0;
}
/**
* Removes all given Features from the map and clears all stored features.
*
* @param features features to remove
*/
protected void removeFeatures(HashMap<? extends Feature, Object> features) {
removeFeatures(features.values());
}
/**
* Removes all given Features from the map and clears all stored features.
*
* @param features features to remove
*/
private void removeFeatures(Collection features) {
// Remove map object from the map
for (Object mapObject : features) {
if (mapObject instanceof Collection) {
removeFeatures((Collection) mapObject);
} else if (mapObject instanceof Marker) {
mMarkers.remove((Marker) mapObject);
} else if (mapObject instanceof Polyline) {
mPolylines.remove((Polyline) mapObject);
} else if (mapObject instanceof Polygon) {
mPolygons.remove((Polygon) mapObject);
}
}
}
/**
* Removes all ground overlays in the given hashmap
*
* @param groundOverlays hashmap of ground overlays to remove
*/
protected void removeGroundOverlays(HashMap<KmlGroundOverlay, GroundOverlay> groundOverlays) {
for (GroundOverlay groundOverlay : groundOverlays.values()) {
// Ground overlay values may be null if their image was not yet downloaded
if (groundOverlay != null) {
mGroundOverlays.remove(groundOverlay);
}
}
}
/**
* Removes a Feature from the map if its geometry property is not null
*
* @param feature feature to remove from map
*/
protected void removeFeature(Feature feature) {
// Check if given feature is stored
if (mFeatures.containsKey(feature)) {
removeFromMap(mFeatures.remove(feature));
}
}
/**
* Checks for each style in the feature and adds a default style if none is applied
*
* @param feature feature to apply default styles to
*/
private void setFeatureDefaultStyles(GeoJsonFeature feature) {
if (feature.getPointStyle() == null) {
feature.setPointStyle(mDefaultPointStyle);
}
if (feature.getLineStringStyle() == null) {
feature.setLineStringStyle(mDefaultLineStringStyle);
}
if (feature.getPolygonStyle() == null) {
feature.setPolygonStyle(mDefaultPolygonStyle);
}
}
/**
* Removes all the mappings from the mStylesRenderer hashmap
*/
protected void clearStylesRenderer() {
mStylesRenderer.clear();
}
/**
* Stores all given data
*
* @param styles hashmap of styles
* @param styleMaps hashmap of style maps
* @param features hashmap of features
* @param folders array of containers
* @param groundOverlays hashmap of ground overlays
*/
protected void storeData(HashMap<String, KmlStyle> styles,
HashMap<String, String> styleMaps,
HashMap<KmlPlacemark, Object> features,
ArrayList<KmlContainer> folders,
HashMap<KmlGroundOverlay, GroundOverlay> groundOverlays) {
mStyles = styles;
mStyleMaps = styleMaps;
mFeatures.putAll(features);
mContainers = folders;
mGroundOverlayMap = groundOverlays;
}
/**
* Adds a new Feature to the map if its geometry property is not null.
*
* @param feature feature to add to the map
*/
protected void addFeature(Feature feature) {
Object mapObject = FEATURE_NOT_ON_MAP;
if (feature instanceof GeoJsonFeature) {
setFeatureDefaultStyles((GeoJsonFeature) feature);
}
if (mLayerOnMap) {
if (mFeatures.containsKey(feature)) {
// Remove current map objects before adding new ones
removeFromMap(mFeatures.get(feature));
}
if (feature.hasGeometry()) {
// Create new map object
if (feature instanceof KmlPlacemark) {
boolean isPlacemarkVisible = getPlacemarkVisibility(feature);
String placemarkId = feature.getId();
Geometry geometry = feature.getGeometry();
KmlStyle style = getPlacemarkStyle(placemarkId);
KmlStyle inlineStyle = ((KmlPlacemark) feature).getInlineStyle();
mapObject = addKmlPlacemarkToMap((KmlPlacemark) feature, geometry, style, inlineStyle, isPlacemarkVisible);
} else {
mapObject = addGeoJsonFeatureToMap(feature, feature.getGeometry());
}
}
}
mFeatures.put(feature, mapObject);
}
/**
* Given a Marker, Polyline, Polygon or an array of these and removes it from the map
*
* @param mapObject map object or array of map objects to remove from the map
*/
protected void removeFromMap(Object mapObject) {
if (mapObject instanceof Marker) {
mMarkers.remove((Marker) mapObject);
} else if (mapObject instanceof Polyline) {
mPolylines.remove((Polyline) mapObject);
} else if (mapObject instanceof Polygon) {
mPolygons.remove((Polygon) mapObject);
} else if (mapObject instanceof GroundOverlay) {
mGroundOverlays.remove((GroundOverlay) mapObject);
} else if (mapObject instanceof ArrayList) {
for (Object mapObjectElement : (ArrayList) mapObject) {
removeFromMap(mapObjectElement);
}
}
}
/**
* Adds a new object onto the map using the Geometry for the coordinates and the
* Feature for the styles. (used for GeoJson)
*
* @param feature feature to get geometry style
* @param geometry geometry to add to the map
*/
protected Object addGeoJsonFeatureToMap(Feature feature, Geometry geometry) {
String geometryType = geometry.getGeometryType();
switch (geometryType) {
case "Point":
MarkerOptions markerOptions = null;
if (feature instanceof GeoJsonFeature) {
markerOptions = ((GeoJsonFeature) feature).getMarkerOptions();
} else if (feature instanceof KmlPlacemark) {
markerOptions = ((KmlPlacemark) feature).getMarkerOptions();
}
return addPointToMap(markerOptions, (GeoJsonPoint) geometry);
case "LineString":
PolylineOptions polylineOptions = null;
if (feature instanceof GeoJsonFeature) {
polylineOptions = ((GeoJsonFeature) feature).getPolylineOptions();
} else if (feature instanceof KmlPlacemark) {
polylineOptions = ((KmlPlacemark) feature).getPolylineOptions();
}
return addLineStringToMap(polylineOptions, (GeoJsonLineString) geometry);
case "Polygon":
PolygonOptions polygonOptions = null;
if (feature instanceof GeoJsonFeature) {
polygonOptions = ((GeoJsonFeature) feature).getPolygonOptions();
} else if (feature instanceof KmlPlacemark) {
polygonOptions = ((KmlPlacemark) feature).getPolygonOptions();
}
return addPolygonToMap(polygonOptions, (DataPolygon) geometry);
case "MultiPoint":
return addMultiPointToMap(((GeoJsonFeature) feature).getPointStyle(),
(GeoJsonMultiPoint) geometry);
case "MultiLineString":
return addMultiLineStringToMap(((GeoJsonFeature) feature).getLineStringStyle(),
((GeoJsonMultiLineString) geometry));
case "MultiPolygon":
return addMultiPolygonToMap(((GeoJsonFeature) feature).getPolygonStyle(),
((GeoJsonMultiPolygon) geometry));
case "GeometryCollection":
return addGeometryCollectionToMap(((GeoJsonFeature) feature),
((GeoJsonGeometryCollection) geometry).getGeometries());
}
return null;
}
/**
* Adds a single geometry object to the map with its specified style (used for KML)
*
* @param geometry defines the type of object to add to the map
* @param style defines styling properties to add to the object when added to the map
* @return the object that was added to the map, this is a Marker, Polyline, Polygon or an array
* of either objects
*/
protected Object addKmlPlacemarkToMap(KmlPlacemark placemark, Geometry geometry, KmlStyle style,
KmlStyle inlineStyle, boolean isVisible) {
String geometryType = geometry.getGeometryType();
boolean hasDrawOrder = placemark.hasProperty("drawOrder");
float drawOrder = 0;
if (hasDrawOrder) {
try {
drawOrder = Float.parseFloat(placemark.getProperty("drawOrder"));
} catch (NumberFormatException e) {
hasDrawOrder = false;
}
}
switch (geometryType) {
case "Point":
MarkerOptions markerOptions = style.getMarkerOptions();
if (inlineStyle != null) {
setInlinePointStyle(markerOptions, inlineStyle, style);
} else if (style.getIconUrl() != null) {
// Use shared style
addMarkerIcons(style.getIconUrl(), style.getIconScale(), markerOptions);
}
Marker marker = addPointToMap(markerOptions, (KmlPoint) geometry);
marker.setVisible(isVisible);
setMarkerInfoWindow(style, marker, placemark);
if (hasDrawOrder) {
marker.setZIndex(drawOrder);
}
return marker;
case "LineString":
PolylineOptions polylineOptions = style.getPolylineOptions();
if (inlineStyle != null) {
setInlineLineStringStyle(polylineOptions, inlineStyle);
} else if (style.isLineRandomColorMode()) {
polylineOptions.color(KmlStyle.computeRandomColor(polylineOptions.getColor()));
}
Polyline polyline = addLineStringToMap(polylineOptions, (LineString) geometry);
polyline.setVisible(isVisible);
if (hasDrawOrder) {
polyline.setZIndex(drawOrder);
}
return polyline;
case "Polygon":
PolygonOptions polygonOptions = style.getPolygonOptions();
if (inlineStyle != null) {
setInlinePolygonStyle(polygonOptions, inlineStyle);
} else if (style.isPolyRandomColorMode()) {
polygonOptions.fillColor(KmlStyle.computeRandomColor(polygonOptions.getFillColor()));
}
Polygon polygon = addPolygonToMap(polygonOptions, (DataPolygon) geometry);
polygon.setVisible(isVisible);
if (hasDrawOrder) {
polygon.setZIndex(drawOrder);
}
return polygon;
case "MultiGeometry":
return addMultiGeometryToMap(placemark, (KmlMultiGeometry) geometry, style, inlineStyle,
isVisible);
}
return null;
}
/**
* Adds a Point to the map as a Marker
*
* @param markerOptions contains relevant styling properties for the Marker
* @param point contains coordinates for the Marker
* @return Marker object created from the given Point
*/
private Marker addPointToMap(MarkerOptions markerOptions, Point point) {
markerOptions.position(point.getGeometryObject());
return mMarkers.addMarker(markerOptions);
}
/**
* Sets the inline point style by copying over the styles that have been set
*
* @param markerOptions marker options object to add inline styles to
* @param inlineStyle inline styles to apply
* @param defaultStyle default shared style
*/
private void setInlinePointStyle(MarkerOptions markerOptions, KmlStyle inlineStyle,
KmlStyle defaultStyle) {
MarkerOptions inlineMarkerOptions = inlineStyle.getMarkerOptions();
if (inlineStyle.isStyleSet("heading")) {
markerOptions.rotation(inlineMarkerOptions.getRotation());
}
if (inlineStyle.isStyleSet("hotSpot")) {
markerOptions
.anchor(inlineMarkerOptions.getAnchorU(), inlineMarkerOptions.getAnchorV());
}
if (inlineStyle.isStyleSet("markerColor")) {
markerOptions.icon(inlineMarkerOptions.getIcon());
}
double scale;
if (inlineStyle.isStyleSet("iconScale")) {
scale = inlineStyle.getIconScale();
} else if (defaultStyle.isStyleSet("iconScale")) {
scale = defaultStyle.getIconScale();
} else {
scale = 1.0;
}
if (inlineStyle.isStyleSet("iconUrl")) {
addMarkerIcons(inlineStyle.getIconUrl(), scale, markerOptions);
} else if (defaultStyle.getIconUrl() != null) {
// Inline style with no icon defined
addMarkerIcons(defaultStyle.getIconUrl(), scale, markerOptions);
}
}
/**
* Adds a LineString to the map as a Polyline
*
* @param polylineOptions contains relevant styling properties for the Polyline
* @param lineString contains coordinates for the Polyline
* @return Polyline object created from given LineString
*/
private Polyline addLineStringToMap(PolylineOptions polylineOptions,
LineString lineString) {
// Add coordinates
polylineOptions.addAll(lineString.getGeometryObject());
Polyline addedPolyline = mPolylines.addPolyline(polylineOptions);
addedPolyline.setClickable(polylineOptions.isClickable());
return addedPolyline;
}
/**
* Sets the inline linestring style by copying over the styles that have been set
*
* @param polylineOptions polygon options object to add inline styles to
* @param inlineStyle inline styles to apply
*/
private void setInlineLineStringStyle(PolylineOptions polylineOptions, KmlStyle inlineStyle) {
PolylineOptions inlinePolylineOptions = inlineStyle.getPolylineOptions();
if (inlineStyle.isStyleSet("outlineColor")) {
polylineOptions.color(inlinePolylineOptions.getColor());
}
if (inlineStyle.isStyleSet("width")) {
polylineOptions.width(inlinePolylineOptions.getWidth());
}
if (inlineStyle.isLineRandomColorMode()) {
polylineOptions.color(KmlStyle.computeRandomColor(inlinePolylineOptions.getColor()));
}
}
/**
* Adds a DataPolygon to the map as a Polygon
*
* @param polygonOptions
* @param polygon contains coordinates for the Polygon
* @return Polygon object created from given DataPolygon
*/
private Polygon addPolygonToMap(PolygonOptions polygonOptions, DataPolygon polygon) {
// First array of coordinates are the outline
polygonOptions.addAll(polygon.getOuterBoundaryCoordinates());
// Following arrays are holes
List<List<LatLng>> innerBoundaries = polygon.getInnerBoundaryCoordinates();
for (List<LatLng> innerBoundary : innerBoundaries) {
polygonOptions.addHole(innerBoundary);
}
Polygon addedPolygon = mPolygons.addPolygon(polygonOptions);
addedPolygon.setClickable(polygonOptions.isClickable());
return addedPolygon;
}
/**
* Sets the inline polygon style by copying over the styles that have been set
*
* @param polygonOptions polygon options object to add inline styles to
* @param inlineStyle inline styles to apply
*/
private void setInlinePolygonStyle(PolygonOptions polygonOptions, KmlStyle inlineStyle) {
PolygonOptions inlinePolygonOptions = inlineStyle.getPolygonOptions();
if (inlineStyle.hasFill() && inlineStyle.isStyleSet("fillColor")) {
polygonOptions.fillColor(inlinePolygonOptions.getFillColor());
}
if (inlineStyle.hasOutline()) {
if (inlineStyle.isStyleSet("outlineColor")) {
polygonOptions.strokeColor(inlinePolygonOptions.getStrokeColor());
}
if (inlineStyle.isStyleSet("width")) {
polygonOptions.strokeWidth(inlinePolygonOptions.getStrokeWidth());
}
}
if (inlineStyle.isPolyRandomColorMode()) {
polygonOptions.fillColor(KmlStyle.computeRandomColor(inlinePolygonOptions.getFillColor()));
}
}
/**
* Adds all Geometry objects stored in the GeoJsonGeometryCollection onto the map.
* Supports recursive GeometryCollections.
*
* @param feature contains relevant styling properties for the Geometry inside
* the GeoJsonGeometryCollection
* @param geoJsonGeometries contains an array of Geometry objects
* @return array of Marker, Polyline, Polygons that have been added to the map
*/
private ArrayList<Object> addGeometryCollectionToMap(GeoJsonFeature feature,
List<Geometry> geoJsonGeometries) {
ArrayList<Object> geometries = new ArrayList<>();
for (Geometry geometry : geoJsonGeometries) {
geometries.add(addGeoJsonFeatureToMap(feature, geometry));
}
return geometries;
}
/**
* Gets the visibility of the placemark if it is specified. A visibility value of "1"
* corresponds as "true", a visibility value of "0" corresponds as false. If the
* visibility is not set, the method returns "true".
*
* @param feature Feature to obtain visibility from.
* @return False if a Feature has a visibility value of "1", true otherwise.
*/
protected static boolean getPlacemarkVisibility(Feature feature) {
boolean isFeatureVisible = true;
if (feature.hasProperty("visibility")) {
String placemarkVisibility = feature.getProperty("visibility");
if (Integer.parseInt(placemarkVisibility) == 0) {
isFeatureVisible = false;
}
}
return isFeatureVisible;
}