Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 46 additions & 40 deletions android/src/main/java/com/rnmaps/maps/MapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,21 @@ public synchronized void doDestroy() {
}
destroyed = true;

Activity activity = context.getCurrentActivity();
if (activity instanceof LifecycleOwner) {
((LifecycleOwner) activity).getLifecycle().removeObserver(this);
}
if (!paused) {
onPause();
paused = true;
try {
if (!paused) {
onPause();
paused = true;
}
onDestroy();

Activity activity = context.getCurrentActivity();
if (activity instanceof LifecycleOwner) {
((LifecycleOwner) activity).getLifecycle().removeObserver(this);
}

} catch (Exception exception){
Log.e("MapView", "exception with destroying", exception);
}
onDestroy();
}

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

// Native nearby markers management
public void updateNearbyMarkersFromProcessedData(org.json.JSONArray processedMarkers) {

if (map == null) {
return;
}

try {

java.util.Set<String> seenDrivers = new java.util.HashSet<>();

// Process each marker with its corresponding React view
for (int i = 0; i < processedMarkers.length(); i++) {
org.json.JSONObject markerData = processedMarkers.getJSONObject(i);

String driverId = markerData.getString("id");
double lat = markerData.getDouble("latitude");
double lon = markerData.getDouble("longitude");
double rotation = markerData.optDouble("rotation", -1);
int zIndex = markerData.optInt("zIndex", 0);
String vehicleVariant = markerData.optString("vehicleVariant", "auto");

seenDrivers.add(driverId);

Marker existingMarker = nearbyMarkersCache.get(driverId);

if (existingMarker != null) {
// Update existing marker
existingMarker.setPosition(new com.google.android.gms.maps.model.LatLng(lat, lon));
existingMarker.setZIndex(zIndex);

// Update icon if vehicle variant changed
existingMarker.setIcon(getIconFromAssets(vehicleVariant, rotation));

} else {
// Create new marker with custom view
com.google.android.gms.maps.model.MarkerOptions markerOptions = new com.google.android.gms.maps.model.MarkerOptions()
Expand All @@ -1944,18 +1950,18 @@ public void updateNearbyMarkersFromProcessedData(org.json.JSONArray processedMar
.zIndex(zIndex)
.flat(true)
.visible(true);


// Use custom marker icon based on vehicle variant and rotation
markerOptions.icon(getIconFromAssets(vehicleVariant, rotation));

Marker newMarker = map.addMarker(markerOptions);
if (newMarker != null) {
nearbyMarkersCache.put(driverId, newMarker);
}
}
}

// Remove stale markers
java.util.Iterator<java.util.Map.Entry<String, Marker>> iterator = nearbyMarkersCache.entrySet().iterator();
while (iterator.hasNext()) {
Expand All @@ -1967,8 +1973,8 @@ public void updateNearbyMarkersFromProcessedData(org.json.JSONArray processedMar
iterator.remove();
}
}


} catch (Exception e) {
Log.e("RNMaps_NearbyMarkers", "Error in updateNearbyMarkersFromProcessedData: " + e.getMessage(), e);
}
Expand All @@ -1979,17 +1985,17 @@ private com.google.android.gms.maps.model.BitmapDescriptor getIconFromAssets(Str
try {
// Transform vehicle variant to lowercase
String normalizedVariant = vehicleVariant.toLowerCase();

// Get the nearest rotation angle
int nearestRotation = getNearestRotationAngle(rotation);

// Build asset name with lowercase variant (e.g., "mt_ic_auto_90", "mt_ic_bike_30")
String assetName = "mt_ic_" + normalizedVariant + "_" + nearestRotation;


// Get resource ID using getIdentifier
int resourceId = getResources().getIdentifier(assetName, "drawable", getContext().getPackageName());

if (resourceId != 0) {
// Resource found, create scaled bitmap
return createScaledBitmapDescriptor(resourceId);
Expand All @@ -1999,7 +2005,7 @@ private com.google.android.gms.maps.model.BitmapDescriptor getIconFromAssets(Str
com.google.android.gms.maps.model.BitmapDescriptorFactory.HUE_BLUE
);
}

} catch (Exception e) {
Log.e("RNMaps_NearbyMarkers", "Error getting icon: " + e.getMessage());
return com.google.android.gms.maps.model.BitmapDescriptorFactory.defaultMarker(
Expand All @@ -2015,7 +2021,7 @@ private com.google.android.gms.maps.model.BitmapDescriptor createScaledBitmapDes
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false; // Disable automatic scaling based on resource density
android.graphics.Bitmap originalBitmap = android.graphics.BitmapFactory.decodeResource(getResources(), resourceId,options);

if (originalBitmap != null) {

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

Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, widthPx, heightPx, true);

// Recycle original bitmap to free memory
originalBitmap.recycle();

// Create bitmap descriptor from scaled bitmap
return com.google.android.gms.maps.model.BitmapDescriptorFactory.fromBitmap(scaledBitmap);

} else {
// Fallback to default if bitmap loading fails
return com.google.android.gms.maps.model.BitmapDescriptorFactory.defaultMarker(
com.google.android.gms.maps.model.BitmapDescriptorFactory.HUE_BLUE
);
}

} catch (Exception e) {
Log.e("RNMaps_NearbyMarkers", "Error creating scaled bitmap: " + e.getMessage());
return com.google.android.gms.maps.model.BitmapDescriptorFactory.defaultMarker(
Expand All @@ -2051,25 +2057,25 @@ private com.google.android.gms.maps.model.BitmapDescriptor createScaledBitmapDes
// Get the nearest rotation angle
private int getNearestRotationAngle(double angle) {
if (angle == -1) return 90;

double normalizedAngle = ((angle % 360) + 360) % 360;
int[] availableAngles = {0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330};

int closestAngle = availableAngles[0];
double minDifference = 360;

for (int availableAngle : availableAngles) {
double difference = Math.min(
Math.abs(normalizedAngle - availableAngle),
360 - Math.abs(normalizedAngle - availableAngle)
);

if (difference < minDifference) {
minDifference = difference;
closestAngle = availableAngle;
}
}

return closestAngle;
}

Expand Down
Loading