Skip to content

Commit 1a7ad87

Browse files
gilboxExilz
authored andcommitted
[ios][google] implement fitToSuppliedMarkers and fitToCoordinates (react-native-maps#750)
1 parent 72ffa49 commit 1a7ad87

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

example/App.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ class App extends React.Component {
135135
[TakeSnapshot, 'Take Snapshot', true, '(incomplete)'],
136136
[CachedMap, 'Cached Map'],
137137
[LoadingMap, 'Map with loading'],
138-
[FitToSuppliedMarkers, 'Focus Map On Markers'],
139-
[FitToCoordinates, 'Fit Map To Coordinates'],
138+
[FitToSuppliedMarkers, 'Focus Map On Markers', true],
139+
[FitToCoordinates, 'Fit Map To Coordinates', true],
140140
[LiteMapView, 'Android Lite MapView'],
141141
[CustomTiles, 'Custom Tiles'],
142142
[ZIndexMarkers, 'Position Markers with Z-index', true],

ios/AirGoogleMaps/AIRGoogleMapManager.m

+58
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,64 @@ - (UIView *)view
8585
}];
8686
}
8787

88+
RCT_EXPORT_METHOD(fitToSuppliedMarkers:(nonnull NSNumber *)reactTag
89+
markers:(nonnull NSArray *)markers
90+
animated:(BOOL)animated)
91+
{
92+
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
93+
id view = viewRegistry[reactTag];
94+
if (![view isKindOfClass:[AIRGoogleMap class]]) {
95+
RCTLogError(@"Invalid view returned from registry, expecting AIRGoogleMap, got: %@", view);
96+
} else {
97+
AIRGoogleMap *mapView = (AIRGoogleMap *)view;
98+
99+
NSPredicate *filterMarkers = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
100+
AIRGoogleMapMarker *marker = (AIRGoogleMapMarker *)evaluatedObject;
101+
return [marker isKindOfClass:[AIRGoogleMapMarker class]] && [markers containsObject:marker.identifier];
102+
}];
103+
104+
NSArray *filteredMarkers = [mapView.markers filteredArrayUsingPredicate:filterMarkers];
105+
106+
CLLocationCoordinate2D myLocation = ((AIRGoogleMapMarker *)(filteredMarkers.firstObject)).realMarker.position;
107+
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:myLocation coordinate:myLocation];
108+
109+
for (AIRGoogleMapMarker *marker in filteredMarkers)
110+
bounds = [bounds includingCoordinate:marker.realMarker.position];
111+
112+
[mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:55.0f]];
113+
}
114+
}];
115+
}
116+
117+
RCT_EXPORT_METHOD(fitToCoordinates:(nonnull NSNumber *)reactTag
118+
coordinates:(nonnull NSArray<AIRMapCoordinate *> *)coordinates
119+
edgePadding:(nonnull NSDictionary *)edgePadding
120+
animated:(BOOL)animated)
121+
{
122+
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
123+
id view = viewRegistry[reactTag];
124+
if (![view isKindOfClass:[AIRGoogleMap class]]) {
125+
RCTLogError(@"Invalid view returned from registry, expecting AIRGoogleMap, got: %@", view);
126+
} else {
127+
AIRGoogleMap *mapView = (AIRGoogleMap *)view;
128+
129+
CLLocationCoordinate2D myLocation = coordinates.firstObject.coordinate;
130+
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:myLocation coordinate:myLocation];
131+
132+
for (AIRMapCoordinate *coordinate in coordinates)
133+
bounds = [bounds includingCoordinate:coordinate.coordinate];
134+
135+
// Set Map viewport
136+
CGFloat top = [RCTConvert CGFloat:edgePadding[@"top"]];
137+
CGFloat right = [RCTConvert CGFloat:edgePadding[@"right"]];
138+
CGFloat bottom = [RCTConvert CGFloat:edgePadding[@"bottom"]];
139+
CGFloat left = [RCTConvert CGFloat:edgePadding[@"left"]];
140+
141+
[mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)]];
142+
}
143+
}];
144+
}
145+
88146
RCT_EXPORT_METHOD(takeSnapshot:(nonnull NSNumber *)reactTag
89147
withWidth:(nonnull NSNumber *)width
90148
withHeight:(nonnull NSNumber *)height

0 commit comments

Comments
 (0)