Skip to content

Commit

Permalink
feat(googleMaps): add support for the new Google's cloud based maps /…
Browse files Browse the repository at this point in the history
… styling via googleMapId prop

* GoogleMapConfig as initialProps for MapView
- Android: LiteMode and mapID as InitProps
- Android: remove unnecessary module for MapLite
- Examples: LiteMode working with initialProps
formatting: whitespace cleanup

* GoogleMapConfig as initialProps for MapView
- iOS: code works to pass mapID to googleMaps
- iOS: swizzling RCTComponentData to add support for initialProps
- iOS: Any RCTViewManager will automatically have initialProps autofilled if property is available

* GoogleMapConfig as initialProps for MapView
- remove unused import from MapView

---------

authored-by: salah ghanim <salah.ghanim@gmail.com>
  • Loading branch information
salah-ghanim committed Jan 2, 2024
1 parent 5843641 commit 77610e9
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 148 deletions.
20 changes: 0 additions & 20 deletions android/src/main/java/com/rnmaps/maps/MapLiteManager.java

This file was deleted.

28 changes: 27 additions & 1 deletion android/src/main/java/com/rnmaps/maps/MapManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.facebook.react.common.MapBuilder;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.ReactStylesDiffMap;
import com.facebook.react.uimanager.StateWrapper;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;
Expand Down Expand Up @@ -67,9 +69,21 @@ public String getName() {
}

@Override
protected MapView createViewInstance(ThemedReactContext context) {
protected MapView createViewInstance(@NonNull ThemedReactContext context) {
return new MapView(context, this.appContext, this, googleMapOptions);
}
@Override
protected MapView createViewInstance(int reactTag, @NonNull ThemedReactContext reactContext, @Nullable ReactStylesDiffMap initialProps, @Nullable StateWrapper stateWrapper) {
if (initialProps != null){
if (initialProps.getString("googleMapId") != null) {
googleMapOptions.mapId(initialProps.getString("googleMapId"));
}
if (initialProps.hasKey("liteMode")) {
googleMapOptions.liteMode(initialProps.getBoolean("liteMode", false));
}
}
return super.createViewInstance(reactTag, reactContext, initialProps, stateWrapper);
}

private void emitMapError(ThemedReactContext context, String message, String type) {
WritableMap error = Arguments.createMap();
Expand All @@ -86,6 +100,18 @@ public void setRegion(MapView view, ReadableMap region) {
view.setRegion(region);
}

@ReactProp(name = "liteMode", defaultBoolean = false)
public void setLiteMode(MapView view, boolean liteMode) {
googleMapOptions.liteMode(liteMode);
}

@ReactProp(name = "googleMapId")
public void setGoogleMapId(MapView view, @Nullable String googleMapId) {
if (googleMapId != null) {
googleMapOptions.mapId(googleMapId);
}
}

@ReactProp(name = "initialRegion")
public void setInitialRegion(MapView view, ReadableMap initialRegion) {
view.setInitialRegion(initialRegion);
Expand Down
1 change: 0 additions & 1 deletion android/src/main/java/com/rnmaps/maps/MapsPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public List<ViewManager> createViewManagers(ReactApplicationContext reactContext
viewManagers.add(new MapGradientPolylineManager(reactContext));
viewManagers.add(new MapPolygonManager(reactContext));
viewManagers.add(new MapCircleManager(reactContext));
viewManagers.add(new MapLiteManager(reactContext));
viewManagers.add(new MapUrlTileManager(reactContext));
viewManagers.add(new MapWMSTileManager(reactContext));
viewManagers.add(new MapLocalTileManager(reactContext));
Expand Down
1 change: 1 addition & 0 deletions ios/AirGoogleMaps/AIRGoogleMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
+ (GMSCameraPosition*)makeGMSCameraPositionFromMap:(GMSMapView *)map andMKCoordinateRegion:(MKCoordinateRegion)region;

- (NSDictionary*) getMarkersFramesWithOnlyVisible:(BOOL)onlyVisible;
- (instancetype) initWithMapId:(NSString *) mapId;

@end

Expand Down
22 changes: 20 additions & 2 deletions ios/AirGoogleMaps/AIRGoogleMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,21 @@ @implementation AIRGoogleMap
BOOL _didPrepareMap;
BOOL _didCallOnMapReady;
BOOL _zoomTapEnabled;
NSString* _googleMapId;
}

- (instancetype)init
- (instancetype)initWithMapId:(NSString *)mapId
{
if ((self = [super init])) {
if (mapId){
GMSMapID *mapID = [GMSMapID mapIDWithIdentifier:mapId];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:47.0169
longitude:-122.336471
zoom:12];
self = [super initWithFrame:CGRectZero mapID:mapID camera:camera];
} else {
self = [super init];
}
if (self) {
_reactSubviews = [NSMutableArray new];
_markers = [NSMutableArray array];
_polygons = [NSMutableArray array];
Expand Down Expand Up @@ -105,6 +115,10 @@ - (instancetype)init
return self;
}

- (instancetype) init {
return [self initWithMapId:nil];
}

- (void)dealloc {
[self removeObserver:self
forKeyPath:@"myLocation"
Expand Down Expand Up @@ -292,6 +306,10 @@ - (void)setRegion:(MKCoordinateRegion)region {
}
}

- (void) setGoogleMapId:(NSString *) googleMapId {
_googleMapId = googleMapId;
}

- (void)setCameraProp:(GMSCameraPosition*)camera {
_initialCamera = camera;
self.camera = camera;
Expand Down
3 changes: 3 additions & 0 deletions ios/AirGoogleMaps/AIRGoogleMapManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#import <React/RCTViewManager.h>

@interface AIRGoogleMapManager : RCTViewManager

@property (nonatomic, strong) NSDictionary *initialProps;

@property (nonatomic) BOOL isGesture;

@end
Expand Down
8 changes: 6 additions & 2 deletions ios/AirGoogleMaps/AIRGoogleMapManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ @implementation AIRGoogleMapManager
- (UIView *)view
{
[GMSServices setMetalRendererEnabled:YES];

AIRGoogleMap *map = [AIRGoogleMap new];
NSString* googleMapId = nil;
if (self.initialProps && self.initialProps[@"googleMapId"]){
googleMapId = self.initialProps[@"googleMapId"];
}
AIRGoogleMap *map = [[AIRGoogleMap alloc] initWithMapId:googleMapId];
map.bridge = self.bridge;
map.delegate = self;
map.isAccessibilityElement = NO;
Expand All @@ -67,6 +70,7 @@ - (UIView *)view

RCT_EXPORT_VIEW_PROPERTY(isAccessibilityElement, BOOL)
RCT_REMAP_VIEW_PROPERTY(testID, accessibilityIdentifier, NSString)
RCT_EXPORT_VIEW_PROPERTY(googleMapId, NSString)
RCT_EXPORT_VIEW_PROPERTY(initialCamera, GMSCameraPosition)
RCT_REMAP_VIEW_PROPERTY(camera, cameraProp, GMSCameraPosition)
RCT_EXPORT_VIEW_PROPERTY(initialRegion, MKCoordinateRegion)
Expand Down
6 changes: 6 additions & 0 deletions ios/AirMaps.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
2163AA501FEAEDD100BBEC95 /* AIRMapPolylineRenderer.m in Sources */ = {isa = PBXBuildFile; fileRef = 2163AA4F1FEAEDD100BBEC95 /* AIRMapPolylineRenderer.m */; };
4C99C9DE2226CF2800A8693E /* AIRWeakTimerReference.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C99C9DD2226CF2800A8693E /* AIRWeakTimerReference.m */; };
4C99C9E12226D8C400A8693E /* AIRWeakMapReference.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C99C9E02226D8C400A8693E /* AIRWeakMapReference.m */; };
4E0CFBDE2B388F2B0017E126 /* RCTComponentData+Maps.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E0CFBDD2B388F2B0017E126 /* RCTComponentData+Maps.m */; };
53D31636202E723B00B55447 /* AIRMapOverlayManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 53D31635202E723B00B55447 /* AIRMapOverlayManager.m */; };
53D3163A202E72FC00B55447 /* AIRMapOverlay.m in Sources */ = {isa = PBXBuildFile; fileRef = 53D31639202E72FC00B55447 /* AIRMapOverlay.m */; };
53D3163D202E734F00B55447 /* AIRMapOverlayRenderer.m in Sources */ = {isa = PBXBuildFile; fileRef = 53D3163C202E734F00B55447 /* AIRMapOverlayRenderer.m */; };
Expand Down Expand Up @@ -112,6 +113,8 @@
4C99C9DD2226CF2800A8693E /* AIRWeakTimerReference.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AIRWeakTimerReference.m; sourceTree = "<group>"; };
4C99C9DF2226D8C400A8693E /* AIRWeakMapReference.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AIRWeakMapReference.h; sourceTree = "<group>"; };
4C99C9E02226D8C400A8693E /* AIRWeakMapReference.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AIRWeakMapReference.m; sourceTree = "<group>"; };
4E0CFBDC2B388F2B0017E126 /* RCTComponentData+Maps.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RCTComponentData+Maps.h"; sourceTree = "<group>"; };
4E0CFBDD2B388F2B0017E126 /* RCTComponentData+Maps.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "RCTComponentData+Maps.m"; sourceTree = "<group>"; };
53D31635202E723B00B55447 /* AIRMapOverlayManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AIRMapOverlayManager.m; sourceTree = "<group>"; };
53D31637202E725E00B55447 /* AIRMapOverlayManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AIRMapOverlayManager.h; sourceTree = "<group>"; };
53D31638202E72D500B55447 /* AIRMapOverlay.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AIRMapOverlay.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -217,6 +220,8 @@
11FA5C531C4A1296003AC2EE /* AirMaps */ = {
isa = PBXGroup;
children = (
4E0CFBDC2B388F2B0017E126 /* RCTComponentData+Maps.h */,
4E0CFBDD2B388F2B0017E126 /* RCTComponentData+Maps.m */,
BE2E4EC72621F63C00CC7F2E /* AIRMapUrlTileCachedOverlay.h */,
BE2E4EC82621F63C00CC7F2E /* AIRMapUrlTileCachedOverlay.m */,
1125B2BD1C4AD3DA007D0023 /* AIRMap.h */,
Expand Down Expand Up @@ -399,6 +404,7 @@
A8494E28218891020092506D /* AIRMapWMSTileManager.m in Sources */,
1125B2DB1C4AD3DA007D0023 /* AIRMapCallout.m in Sources */,
53D31636202E723B00B55447 /* AIRMapOverlayManager.m in Sources */,
4E0CFBDE2B388F2B0017E126 /* RCTComponentData+Maps.m in Sources */,
1125B2E01C4AD3DA007D0023 /* AIRMapManager.m in Sources */,
1125B2E61C4AD3DA007D0023 /* AIRMapPolylineManager.m in Sources */,
9B9498DA2017EFB800158761 /* AIRGoogleMapPolygon.m in Sources */,
Expand Down
54 changes: 1 addition & 53 deletions ios/AirMaps/AIRMapCircle.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,56 +114,4 @@ - (BOOL)canReplaceMapContent
return NO;
}





















































@end
@end
6 changes: 2 additions & 4 deletions ios/AirMaps/AIRMapCoordinate.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@
#import "AIRMapCoordinate.h"


@implementation AIRMapCoordinate {

}
@end
@implementation AIRMapCoordinate
@end
52 changes: 0 additions & 52 deletions ios/AirMaps/AIRMapPolygon.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,56 +123,4 @@ - (BOOL)canReplaceMapContent
return NO;
}





















































@end
13 changes: 13 additions & 0 deletions ios/AirMaps/RCTComponentData+Maps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// RCTComponentData+Maps.h
// AirMaps
//
// Created by Salah Ghanim on 24.12.23.
// Copyright © 2023 Christopher. All rights reserved.
//

#import <React/RCTComponentData.h>

@interface RCTComponentData (Maps)

@end
55 changes: 55 additions & 0 deletions ios/AirMaps/RCTComponentData+Maps.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// RCTComponentData+Maps.m
// AirMaps
//
// Created by Salah Ghanim on 24.12.23.
// Copyright © 2023 Christopher. All rights reserved.
//

#import "RCTComponentData+Maps.h"
#import <objc/runtime.h>
#import <Foundation/NSObjCRuntime.h>

@implementation RCTComponentData (Maps)


- (void) myCustom_setProps:(NSDictionary<NSString *, id> *)props forShadowView:(RCTShadowView *)shadowView{
// Pass initialProps to any manager that supports initialProps
id manager = [self manager];
if ([manager respondsToSelector:@selector(setInitialProps:)]) {
[manager performSelector:@selector(setInitialProps:) withObject:props];
}

// Call the original method
[self myCustom_setProps:props forShadowView:shadowView];
}

+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [RCTComponentData class]; // Or the class where the method is defined

SEL originalSelector = @selector(setProps:forShadowView:);
SEL swizzledSelector = @selector(myCustom_setProps:forShadowView:);

Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

BOOL didAddMethod = class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));

if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}


@end
Loading

0 comments on commit 77610e9

Please sign in to comment.