Skip to content

Commit efcaa4f

Browse files
committed
fix: doDestroy crash
1 parent a158d00 commit efcaa4f

1 file changed

Lines changed: 46 additions & 40 deletions

File tree

android/src/main/java/com/rnmaps/maps/MapView.java

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -695,15 +695,21 @@ public synchronized void doDestroy() {
695695
}
696696
destroyed = true;
697697

698-
Activity activity = context.getCurrentActivity();
699-
if (activity instanceof LifecycleOwner) {
700-
((LifecycleOwner) activity).getLifecycle().removeObserver(this);
701-
}
702-
if (!paused) {
703-
onPause();
704-
paused = true;
698+
try {
699+
if (!paused) {
700+
onPause();
701+
paused = true;
702+
}
703+
onDestroy();
704+
705+
Activity activity = context.getCurrentActivity();
706+
if (activity instanceof LifecycleOwner) {
707+
((LifecycleOwner) activity).getLifecycle().removeObserver(this);
708+
}
709+
710+
} catch (Exception exception){
711+
Log.e("MapView", "exception with destroying", exception);
705712
}
706-
onDestroy();
707713
}
708714

709715
public void setInitialRegion(ReadableMap initialRegion) {
@@ -1904,38 +1910,38 @@ private boolean safeRemoveFeatureFromAttacherGroup(MapFeature feature) {
19041910

19051911
// Native nearby markers management
19061912
public void updateNearbyMarkersFromProcessedData(org.json.JSONArray processedMarkers) {
1907-
1913+
19081914
if (map == null) {
19091915
return;
19101916
}
1911-
1917+
19121918
try {
1913-
1919+
19141920
java.util.Set<String> seenDrivers = new java.util.HashSet<>();
1915-
1921+
19161922
// Process each marker with its corresponding React view
19171923
for (int i = 0; i < processedMarkers.length(); i++) {
19181924
org.json.JSONObject markerData = processedMarkers.getJSONObject(i);
1919-
1925+
19201926
String driverId = markerData.getString("id");
19211927
double lat = markerData.getDouble("latitude");
19221928
double lon = markerData.getDouble("longitude");
19231929
double rotation = markerData.optDouble("rotation", -1);
19241930
int zIndex = markerData.optInt("zIndex", 0);
19251931
String vehicleVariant = markerData.optString("vehicleVariant", "auto");
1926-
1932+
19271933
seenDrivers.add(driverId);
1928-
1934+
19291935
Marker existingMarker = nearbyMarkersCache.get(driverId);
1930-
1936+
19311937
if (existingMarker != null) {
19321938
// Update existing marker
19331939
existingMarker.setPosition(new com.google.android.gms.maps.model.LatLng(lat, lon));
19341940
existingMarker.setZIndex(zIndex);
1935-
1941+
19361942
// Update icon if vehicle variant changed
19371943
existingMarker.setIcon(getIconFromAssets(vehicleVariant, rotation));
1938-
1944+
19391945
} else {
19401946
// Create new marker with custom view
19411947
com.google.android.gms.maps.model.MarkerOptions markerOptions = new com.google.android.gms.maps.model.MarkerOptions()
@@ -1944,18 +1950,18 @@ public void updateNearbyMarkersFromProcessedData(org.json.JSONArray processedMar
19441950
.zIndex(zIndex)
19451951
.flat(true)
19461952
.visible(true);
1947-
1948-
1953+
1954+
19491955
// Use custom marker icon based on vehicle variant and rotation
19501956
markerOptions.icon(getIconFromAssets(vehicleVariant, rotation));
1951-
1957+
19521958
Marker newMarker = map.addMarker(markerOptions);
19531959
if (newMarker != null) {
19541960
nearbyMarkersCache.put(driverId, newMarker);
19551961
}
19561962
}
19571963
}
1958-
1964+
19591965
// Remove stale markers
19601966
java.util.Iterator<java.util.Map.Entry<String, Marker>> iterator = nearbyMarkersCache.entrySet().iterator();
19611967
while (iterator.hasNext()) {
@@ -1967,8 +1973,8 @@ public void updateNearbyMarkersFromProcessedData(org.json.JSONArray processedMar
19671973
iterator.remove();
19681974
}
19691975
}
1970-
1971-
1976+
1977+
19721978
} catch (Exception e) {
19731979
Log.e("RNMaps_NearbyMarkers", "Error in updateNearbyMarkersFromProcessedData: " + e.getMessage(), e);
19741980
}
@@ -1979,17 +1985,17 @@ private com.google.android.gms.maps.model.BitmapDescriptor getIconFromAssets(Str
19791985
try {
19801986
// Transform vehicle variant to lowercase
19811987
String normalizedVariant = vehicleVariant.toLowerCase();
1982-
1988+
19831989
// Get the nearest rotation angle
19841990
int nearestRotation = getNearestRotationAngle(rotation);
1985-
1991+
19861992
// Build asset name with lowercase variant (e.g., "mt_ic_auto_90", "mt_ic_bike_30")
19871993
String assetName = "mt_ic_" + normalizedVariant + "_" + nearestRotation;
1988-
1989-
1994+
1995+
19901996
// Get resource ID using getIdentifier
19911997
int resourceId = getResources().getIdentifier(assetName, "drawable", getContext().getPackageName());
1992-
1998+
19931999
if (resourceId != 0) {
19942000
// Resource found, create scaled bitmap
19952001
return createScaledBitmapDescriptor(resourceId);
@@ -1999,7 +2005,7 @@ private com.google.android.gms.maps.model.BitmapDescriptor getIconFromAssets(Str
19992005
com.google.android.gms.maps.model.BitmapDescriptorFactory.HUE_BLUE
20002006
);
20012007
}
2002-
2008+
20032009
} catch (Exception e) {
20042010
Log.e("RNMaps_NearbyMarkers", "Error getting icon: " + e.getMessage());
20052011
return com.google.android.gms.maps.model.BitmapDescriptorFactory.defaultMarker(
@@ -2015,7 +2021,7 @@ private com.google.android.gms.maps.model.BitmapDescriptor createScaledBitmapDes
20152021
BitmapFactory.Options options = new BitmapFactory.Options();
20162022
options.inScaled = false; // Disable automatic scaling based on resource density
20172023
android.graphics.Bitmap originalBitmap = android.graphics.BitmapFactory.decodeResource(getResources(), resourceId,options);
2018-
2024+
20192025
if (originalBitmap != null) {
20202026

20212027
int targetWidthDp = 60; // only width in dp
@@ -2026,20 +2032,20 @@ private com.google.android.gms.maps.model.BitmapDescriptor createScaledBitmapDes
20262032
int heightPx = (int) (widthPx * aspectRatio);
20272033

20282034
Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, widthPx, heightPx, true);
2029-
2035+
20302036
// Recycle original bitmap to free memory
20312037
originalBitmap.recycle();
2032-
2038+
20332039
// Create bitmap descriptor from scaled bitmap
20342040
return com.google.android.gms.maps.model.BitmapDescriptorFactory.fromBitmap(scaledBitmap);
2035-
2041+
20362042
} else {
20372043
// Fallback to default if bitmap loading fails
20382044
return com.google.android.gms.maps.model.BitmapDescriptorFactory.defaultMarker(
20392045
com.google.android.gms.maps.model.BitmapDescriptorFactory.HUE_BLUE
20402046
);
20412047
}
2042-
2048+
20432049
} catch (Exception e) {
20442050
Log.e("RNMaps_NearbyMarkers", "Error creating scaled bitmap: " + e.getMessage());
20452051
return com.google.android.gms.maps.model.BitmapDescriptorFactory.defaultMarker(
@@ -2051,25 +2057,25 @@ private com.google.android.gms.maps.model.BitmapDescriptor createScaledBitmapDes
20512057
// Get the nearest rotation angle
20522058
private int getNearestRotationAngle(double angle) {
20532059
if (angle == -1) return 90;
2054-
2060+
20552061
double normalizedAngle = ((angle % 360) + 360) % 360;
20562062
int[] availableAngles = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};
2057-
2063+
20582064
int closestAngle = availableAngles[0];
20592065
double minDifference = 360;
2060-
2066+
20612067
for (int availableAngle : availableAngles) {
20622068
double difference = Math.min(
20632069
Math.abs(normalizedAngle - availableAngle),
20642070
360 - Math.abs(normalizedAngle - availableAngle)
20652071
);
2066-
2072+
20672073
if (difference < minDifference) {
20682074
minDifference = difference;
20692075
closestAngle = availableAngle;
20702076
}
20712077
}
2072-
2078+
20732079
return closestAngle;
20742080
}
20752081

0 commit comments

Comments
 (0)