Skip to content

Commit

Permalink
fix(android): ui movement when using fitTo{SuppliedMarkers,Coordinates}
Browse files Browse the repository at this point in the history
Resolves #4590
  • Loading branch information
monholm committed May 10, 2023
1 parent e0946c9 commit 657e05f
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions android/src/main/java/com/rnmaps/maps/MapViewModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
promise.reject("RNMMapView.map is not valid");
return;
}
LatLngBounds.Builder builder = new LatLngBounds.Builder();

String[] markerIDs = new String[markersIds.size()];
for (int i = 0; i < markersIds.size(); i++) {
Expand All @@ -510,12 +509,20 @@ public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {

List<String> markerIDList = Arrays.asList(markerIDs);

double maxLatitude = -Double.MAX_VALUE;
double minLatitude = Double.MAX_VALUE;
double maxLongitude = -Double.MAX_VALUE;
double minLongitude = Double.MAX_VALUE;

for (MapFeature feature : view.features) {
if (feature instanceof MapMarker) {
String identifier = ((MapMarker) feature).getIdentifier();
if (markerIDList.contains(identifier)) {
Marker marker = (Marker) feature.getFeature();
builder.include(marker.getPosition());
LatLng position = ((Marker) feature.getFeature()).getPosition();
maxLatitude = Math.max(maxLatitude, position.latitude);
minLatitude = Math.min(minLatitude, position.latitude);
maxLongitude = Math.max(maxLongitude, position.longitude);
minLongitude = Math.min(minLongitude, position.longitude);
addedPosition = true;
}
}
Expand All @@ -525,13 +532,12 @@ public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
// nothing to do
promise.resolve(null);
} else {
LatLngBounds bounds = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, view.baseMapPadding);

if (edgePadding != null) {
view.map.setPadding(edgePadding.getInt("left"), edgePadding.getInt("top"),
edgePadding.getInt("right"), edgePadding.getInt("bottom"));
}
Map<String, Double> paddedBoundary = padBoundary(view, maxLatitude, minLatitude, maxLongitude, minLongitude, edgePadding);
LatLngBounds bounds = new LatLngBounds(
new LatLng(paddedBoundary.get("minLatitude"), paddedBoundary.get("minLongitude")), // southwest
new LatLng(paddedBoundary.get("maxLatitude"), paddedBoundary.get("maxLongitude")) // northeast
);
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);

if (duration > 0) {
view.map.animateCamera(cu, duration, new GoogleMap.CancelableCallback() {
Expand Down Expand Up @@ -569,21 +575,28 @@ public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
return;
}

LatLngBounds.Builder builder = new LatLngBounds.Builder();
double maxLatitude = -Double.MAX_VALUE;
double minLatitude = Double.MAX_VALUE;
double maxLongitude = -Double.MAX_VALUE;
double minLongitude = Double.MAX_VALUE;

for (int i = 0; i < coordinates.size(); i++) {
ReadableMap latLng = coordinates.getMap(i);
double lat = latLng.getDouble("latitude");
double lng = latLng.getDouble("longitude");
builder.include(new LatLng(lat, lng));
maxLatitude = Math.max(maxLatitude, lat);
minLatitude = Math.min(minLatitude, lat);
maxLongitude = Math.max(maxLongitude, lng);
minLongitude = Math.min(minLongitude, lng);
}

LatLngBounds bounds = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, view.baseMapPadding);
Map<String, Double> paddedBoundary = padBoundary(view, maxLatitude, minLatitude, maxLongitude, minLongitude, edgePadding);
LatLngBounds bounds = new LatLngBounds(
new LatLng(paddedBoundary.get("minLatitude"), paddedBoundary.get("minLongitude")), // southwest
new LatLng(paddedBoundary.get("maxLatitude"), paddedBoundary.get("maxLongitude")) // northeast
);

if (edgePadding != null) {
view.appendMapPadding(edgePadding.getInt("left"), edgePadding.getInt("top"), edgePadding.getInt("right"), edgePadding.getInt("bottom"));
}
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);

if (duration > 0) {
view.map.animateCamera(cu, duration, new GoogleMap.CancelableCallback() {
Expand All @@ -601,8 +614,6 @@ public void onFinish() {
view.map.moveCamera(cu);
promise.resolve(null);
}
// Move the google logo to the default base padding value.
view.map.setPadding(view.baseLeftMapPadding, view.baseTopMapPadding, view.baseRightMapPadding, view.baseBottomMapPadding);
}
});
}
Expand Down

0 comments on commit 657e05f

Please sign in to comment.