Skip to content

Commit

Permalink
Implement inset props on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
athaeryn committed Oct 18, 2016
1 parent 154be9e commit 03cc3a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ public void setPitchEnabled(AirMapView view, boolean pitchEnabled) {
view.map.getUiSettings().setTiltGesturesEnabled(pitchEnabled);
}

@ReactProp(name = "insetTop")
public void setInsetTop(AirMapView view, double insetTop) {
view.insetTop = (int) insetTop;
}

@ReactProp(name = "insetBottom")
public void setInsetBottom(AirMapView view, double insetBottom) {
view.insetBottom = (int) insetBottom;
}

@Override
public void receiveCommand(AirMapView view, int commandId, @Nullable ReadableArray args) {
Integer duration;
Expand All @@ -194,7 +204,6 @@ public void receiveCommand(AirMapView view, int commandId, @Nullable ReadableArr
case ANIMATE_TO_REGION:
region = args.getMap(0);
duration = args.getInt(1);
offset = args.getMap(2);
lng = region.getDouble("longitude");
lat = region.getDouble("latitude");
lngDelta = region.getDouble("longitudeDelta");
Expand All @@ -203,7 +212,7 @@ public void receiveCommand(AirMapView view, int commandId, @Nullable ReadableArr
new LatLng(lat - latDelta / 2, lng - lngDelta / 2), // southwest
new LatLng(lat + latDelta / 2, lng + lngDelta / 2) // northeast
);
view.animateToRegion(bounds, duration, (int) offset.getDouble("x"), (int) offset.getDouble("y"));
view.animateToRegion(bounds, duration);
break;

case ANIMATE_TO_COORDINATE:
Expand Down
33 changes: 17 additions & 16 deletions android/src/main/java/com/airbnb/android/react/maps/AirMapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public class AirMapView extends MapView implements GoogleMap.InfoWindowAdapter,
private boolean moveOnMarkerPress = true;
private boolean cacheEnabled = false;

public int insetTop;
public int insetBottom;
public int insetLeft;
public int insetRight;

private static final String[] PERMISSIONS = new String[] {
"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"};

Expand Down Expand Up @@ -452,25 +457,21 @@ public void updateExtraData(Object extraData) {
}
}

public void animateToRegion(LatLngBounds bounds, int duration, int offsetX, int offsetY) {
public void animateToRegion(LatLngBounds bounds, int duration) {
Projection projection = map.getProjection();
Point center = projection.toScreenLocation(bounds.getCenter());
center.offset(offsetX, offsetY);
LatLng offsetCenter = projection.fromScreenLocation(center);

// take the new and subtract the old
LatLng offsetSouthWest = new LatLng(
bounds.southwest.latitude - (offsetCenter.latitude - bounds.getCenter().latitude),
bounds.southwest.longitude - (offsetCenter.longitude - bounds.getCenter().longitude)
);
LatLng offsetNorthEast = new LatLng(
bounds.northeast.latitude - (offsetCenter.latitude - bounds.getCenter().latitude),
bounds.northeast.longitude - (offsetCenter.longitude - bounds.getCenter().longitude)
);
LatLngBounds offsetBounds = new LatLngBounds(offsetSouthWest, offsetNorthEast);
Point northeast = projection.toScreenLocation(bounds.northeast);
Point southwest = projection.toScreenLocation(bounds.southwest);

northeast.offset(-this.insetRight, -this.insetTop);
southwest.offset(this.insetLeft, this.insetBottom);

LatLng insetNorthEast = projection.fromScreenLocation(northeast);
LatLng insetSouthWest = projection.fromScreenLocation(southwest);

LatLngBounds insetBounds = new LatLngBounds(insetSouthWest, insetNorthEast);

startMonitoringRegion();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(offsetBounds, 0), duration, null);
map.animateCamera(CameraUpdateFactory.newLatLngBounds(insetBounds, 0), duration, null);
}

public void animateToCoordinate(LatLng coordinate, int duration) {
Expand Down

0 comments on commit 03cc3a3

Please sign in to comment.