Skip to content

Commit cd40aa3

Browse files
Wrap marker setIcon execute in try/catch to avoid crash in host app
Markers may be invalidated and bitmaps disposed after the initial marker has been created, this can result in a fatal IllegalArgumentException. The exception can cause the host cordova app to crash therefore catching it (and warning via Log) is necessary.
1 parent bbe9a96 commit cd40aa3

1 file changed

Lines changed: 35 additions & 26 deletions

File tree

src/android/plugin/google/maps/PluginMarker.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -759,34 +759,43 @@ protected void onPostExecute(Bitmap image) {
759759
callback.onPostExecute(marker);
760760
return;
761761
}
762-
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(image);
763-
marker.setIcon(bitmapDescriptor);
764-
765-
// Save the information for the anchor property
766-
Bundle imageSize = new Bundle();
767-
imageSize.putInt("width", image.getWidth());
768-
imageSize.putInt("height", image.getHeight());
769-
PluginMarker.this.objects.put("imageSize", imageSize);
770-
771-
772-
// The `anchor` of the `icon` property
773-
if (iconProperty.containsKey("anchor") == true) {
774-
double[] anchor = iconProperty.getDoubleArray("anchor");
775-
if (anchor.length == 2) {
776-
_setIconAnchor(marker, anchor[0], anchor[1], imageSize.getInt("width"), imageSize.getInt("height"));
777-
}
778-
}
779762

763+
try {
764+
//TODO: check image is valid?
765+
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(image);
766+
marker.setIcon(bitmapDescriptor);
767+
768+
// Save the information for the anchor property
769+
Bundle imageSize = new Bundle();
770+
imageSize.putInt("width", image.getWidth());
771+
imageSize.putInt("height", image.getHeight());
772+
PluginMarker.this.objects.put("imageSize", imageSize);
773+
774+
775+
// The `anchor` of the `icon` property
776+
if (iconProperty.containsKey("anchor") == true) {
777+
double[] anchor = iconProperty.getDoubleArray("anchor");
778+
if (anchor.length == 2) {
779+
_setIconAnchor(marker, anchor[0], anchor[1], imageSize.getInt("width"), imageSize.getInt("height"));
780+
}
781+
}
782+
783+
784+
// The `anchor` property for the infoWindow
785+
if (iconProperty.containsKey("infoWindowAnchor") == true) {
786+
double[] anchor = iconProperty.getDoubleArray("infoWindowAnchor");
787+
if (anchor.length == 2) {
788+
_setInfoWindowAnchor(marker, anchor[0], anchor[1], imageSize.getInt("width"), imageSize.getInt("height"));
789+
}
790+
}
791+
792+
callback.onPostExecute(marker);
793+
794+
} catch (java.lang.IllegalArgumentException e) {
795+
Log.e("GoogleMapsPlugin","PluginMarker: Warning - marker method called when marker has been disposed, wait for addMarker callback before calling more methods on the marker (setIcon etc).");
796+
//e.printStackTrace();
780797

781-
// The `anchor` property for the infoWindow
782-
if (iconProperty.containsKey("infoWindowAnchor") == true) {
783-
double[] anchor = iconProperty.getDoubleArray("infoWindowAnchor");
784-
if (anchor.length == 2) {
785-
_setInfoWindowAnchor(marker, anchor[0], anchor[1], imageSize.getInt("width"), imageSize.getInt("height"));
786-
}
787-
}
788-
789-
callback.onPostExecute(marker);
798+
}
790799
}
791800
};
792801
task.execute();

0 commit comments

Comments
 (0)