Skip to content

Commit d639fe4

Browse files
authored
feat: support mapId (#495)
1 parent 2349ab9 commit d639fe4

37 files changed

+2098
-1313
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ in an unbounded widget will cause the application to behave unexpectedly.
287287
// Permissions must have been granted by this point.
288288

289289
<NavigationView
290+
mapId="your-map-id-here" // Optional: Your map ID configured in Google Cloud Console
290291
androidStylingOptions={{
291292
primaryDayModeThemeColor: '#34eba8',
292293
headerDistanceValueTextColor: '#76b5c5',
@@ -310,6 +311,7 @@ You can also add a bare `MapView` that works as a normal map view without naviga
310311

311312
```tsx
312313
<MapView
314+
mapId="your-map-id-here" // Optional: Your map ID configured in Google Cloud Console
313315
mapViewCallbacks={mapViewCallbacks}
314316
onMapViewControllerCreated={setMapViewController}
315317
/>

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ dependencies {
8282
implementation "androidx.car.app:app:1.4.0"
8383
implementation "androidx.car.app:app-projected:1.4.0"
8484
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
85-
implementation "com.google.android.libraries.navigation:navigation:7.0.0"
85+
implementation "com.google.android.libraries.navigation:navigation:7.1.0"
8686
api 'com.google.guava:guava:31.0.1-android'
8787
}

android/src/main/java/com/google/android/react/navsdk/CustomTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package com.google.android.react.navsdk;
1515

1616
public class CustomTypes {
17-
public enum FragmentType {
17+
public enum MapViewType {
1818
MAP,
1919
NAVIGATION
2020
}

android/src/main/java/com/google/android/react/navsdk/EnumTranslationUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ public static int getMapTypeFromJsValue(int jsValue) {
8989
}
9090
}
9191

92-
public static CustomTypes.FragmentType getFragmentTypeFromJsValue(int jsValue) {
92+
public static CustomTypes.MapViewType getMapViewTypeFromJsValue(int jsValue) {
9393
return switch (jsValue) {
94-
case 0 -> CustomTypes.FragmentType.MAP;
95-
case 1 -> CustomTypes.FragmentType.NAVIGATION;
96-
default -> throw new IllegalStateException("Unexpected FragmentType value: " + jsValue);
94+
case 0 -> CustomTypes.MapViewType.MAP;
95+
case 1 -> CustomTypes.MapViewType.NAVIGATION;
96+
default -> throw new IllegalStateException("Unexpected MapViewType value: " + jsValue);
9797
};
9898
}
9999
}

android/src/main/java/com/google/android/react/navsdk/IMapViewFragment.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@
1515

1616
import android.view.View;
1717
import com.google.android.gms.maps.GoogleMap;
18-
import com.google.android.libraries.navigation.StylingOptions;
1918

2019
public interface IMapViewFragment {
2120
MapViewController getMapController();
2221

23-
void setStylingOptions(StylingOptions stylingOptions);
24-
25-
void applyStylingOptions();
26-
2722
void setMapStyle(String url);
2823

2924
GoogleMap getGoogleMap();

android/src/main/java/com/google/android/react/navsdk/INavViewFragment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package com.google.android.react.navsdk;
1515

16+
import com.google.android.libraries.navigation.StylingOptions;
17+
1618
public interface INavViewFragment extends IMapViewFragment {
1719
void setNavigationUiEnabled(boolean enableNavigationUi);
1820

@@ -35,4 +37,8 @@ public interface INavViewFragment extends IMapViewFragment {
3537
void setNightModeOption(int jsValue);
3638

3739
void setReportIncidentButtonEnabled(boolean enabled);
40+
41+
void setStylingOptions(StylingOptions stylingOptions);
42+
43+
void applyStylingOptions();
3844
}

android/src/main/java/com/google/android/react/navsdk/MapViewFragment.java

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,14 @@
2525
import com.facebook.react.uimanager.events.Event;
2626
import com.facebook.react.uimanager.events.EventDispatcher;
2727
import com.google.android.gms.maps.GoogleMap;
28-
import com.google.android.gms.maps.OnMapReadyCallback;
28+
import com.google.android.gms.maps.GoogleMapOptions;
2929
import com.google.android.gms.maps.SupportMapFragment;
3030
import com.google.android.gms.maps.model.Circle;
3131
import com.google.android.gms.maps.model.GroundOverlay;
3232
import com.google.android.gms.maps.model.LatLng;
3333
import com.google.android.gms.maps.model.Marker;
3434
import com.google.android.gms.maps.model.Polygon;
3535
import com.google.android.gms.maps.model.Polyline;
36-
import com.google.android.libraries.navigation.StylingOptions;
37-
import java.util.ArrayList;
38-
import java.util.List;
3936

4037
/**
4138
* A fragment that displays a view with a Google Map using MapFragment. This fragment's lifecycle is
@@ -45,43 +42,45 @@
4542
public class MapViewFragment extends SupportMapFragment
4643
implements IMapViewFragment, INavigationViewCallback {
4744
private static final String TAG = "MapViewFragment";
45+
private int viewTag; // React native view tag.
46+
private ReactApplicationContext reactContext;
4847
private GoogleMap mGoogleMap;
4948
private MapViewController mMapViewController;
50-
private StylingOptions mStylingOptions;
5149

52-
private List<Marker> markerList = new ArrayList<>();
53-
private List<Polyline> polylineList = new ArrayList<>();
54-
private List<Polygon> polygonList = new ArrayList<>();
55-
private List<GroundOverlay> groundOverlayList = new ArrayList<>();
56-
private List<Circle> circleList = new ArrayList<>();
57-
private int viewTag; // React native view tag.
58-
private ReactApplicationContext reactContext;
50+
public static MapViewFragment newInstance(
51+
ReactApplicationContext reactContext, int viewTag, @NonNull GoogleMapOptions mapOptions) {
52+
MapViewFragment fragment = new MapViewFragment();
53+
Bundle args = new Bundle();
54+
args.putParcelable("MapOptions", mapOptions);
5955

60-
public MapViewFragment(ReactApplicationContext reactContext, int viewTag) {
61-
this.reactContext = reactContext;
62-
this.viewTag = viewTag;
63-
}
64-
;
56+
fragment.setArguments(args);
57+
fragment.reactContext = reactContext;
58+
fragment.viewTag = viewTag;
6559

66-
private String style = "";
60+
return fragment;
61+
}
6762

6863
@SuppressLint("MissingPermission")
6964
@Override
7065
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
7166
super.onViewCreated(view, savedInstanceState);
7267

7368
getMapAsync(
74-
new OnMapReadyCallback() {
75-
public void onMapReady(GoogleMap googleMap) {
76-
mGoogleMap = googleMap;
69+
googleMap -> {
70+
mGoogleMap = googleMap;
71+
72+
mMapViewController = new MapViewController();
73+
mMapViewController.initialize(googleMap, this::requireActivity);
7774

78-
mMapViewController = new MapViewController();
79-
mMapViewController.initialize(googleMap, () -> requireActivity());
75+
// Setup map listeners with the provided callback
76+
mMapViewController.setupMapListeners(MapViewFragment.this);
8077

81-
// Setup map listeners with the provided callback
82-
mMapViewController.setupMapListeners(MapViewFragment.this);
78+
emitEvent("onMapReady", null);
8379

84-
emitEvent("onMapReady", null);
80+
// Request layout to ensure fragment is properly sized
81+
View fragmentView = getView();
82+
if (fragmentView != null) {
83+
fragmentView.requestLayout();
8584
}
8685
});
8786
}
@@ -130,10 +129,6 @@ public MapViewController getMapController() {
130129
return mMapViewController;
131130
}
132131

133-
public void applyStylingOptions() {}
134-
135-
public void setStylingOptions(StylingOptions stylingOptions) {}
136-
137132
public void setMapStyle(String url) {
138133
mMapViewController.setMapStyle(url);
139134
}

android/src/main/java/com/google/android/react/navsdk/NavViewFragment.java

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.facebook.react.uimanager.events.Event;
2626
import com.facebook.react.uimanager.events.EventDispatcher;
2727
import com.google.android.gms.maps.GoogleMap;
28-
import com.google.android.gms.maps.OnMapReadyCallback;
28+
import com.google.android.gms.maps.GoogleMapOptions;
2929
import com.google.android.gms.maps.model.Circle;
3030
import com.google.android.gms.maps.model.GroundOverlay;
3131
import com.google.android.gms.maps.model.LatLng;
@@ -44,16 +44,54 @@
4444
public class NavViewFragment extends SupportNavigationFragment
4545
implements INavViewFragment, INavigationViewCallback {
4646
private static final String TAG = "NavViewFragment";
47+
private int viewTag; // React native view tag.
48+
private ReactApplicationContext reactContext;
4749
private MapViewController mMapViewController;
4850
private GoogleMap mGoogleMap;
4951
private StylingOptions mStylingOptions;
5052

51-
private int viewTag; // React native view tag.
52-
private ReactApplicationContext reactContext;
53+
public static NavViewFragment newInstance(
54+
ReactApplicationContext reactContext, int viewTag, @NonNull GoogleMapOptions mapOptions) {
55+
NavViewFragment fragment = new NavViewFragment();
56+
Bundle args = new Bundle();
57+
args.putParcelable("MapOptions", mapOptions);
58+
59+
fragment.setArguments(args);
60+
fragment.reactContext = reactContext;
61+
fragment.viewTag = viewTag;
5362

54-
public NavViewFragment(ReactApplicationContext reactContext, int viewTag) {
55-
this.reactContext = reactContext;
56-
this.viewTag = viewTag;
63+
return fragment;
64+
}
65+
66+
@SuppressLint("MissingPermission")
67+
@Override
68+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
69+
super.onViewCreated(view, savedInstanceState);
70+
71+
setNavigationUiEnabled(NavModule.getInstance().getNavigator() != null);
72+
73+
getMapAsync(
74+
googleMap -> {
75+
mGoogleMap = googleMap;
76+
77+
mMapViewController = new MapViewController();
78+
mMapViewController.initialize(googleMap, this::requireActivity);
79+
80+
// Setup map listeners with the provided callback
81+
mMapViewController.setupMapListeners(NavViewFragment.this);
82+
83+
emitEvent("onMapReady", null);
84+
85+
// Request layout to ensure fragment is properly sized
86+
View fragmentView = getView();
87+
if (fragmentView != null) {
88+
fragmentView.requestLayout();
89+
}
90+
91+
setNavigationUiEnabled(NavModule.getInstance().getNavigator() != null);
92+
addOnRecenterButtonClickedListener(onRecenterButtonClickedListener);
93+
addPromptVisibilityChangedListener(onPromptVisibilityChangedListener);
94+
});
5795
}
5896

5997
private final NavigationView.OnRecenterButtonClickedListener onRecenterButtonClickedListener =
@@ -74,35 +112,6 @@ public void onVisibilityChanged(boolean isVisible) {
74112
}
75113
};
76114

77-
private String style = "";
78-
79-
@SuppressLint("MissingPermission")
80-
@Override
81-
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
82-
super.onViewCreated(view, savedInstanceState);
83-
84-
setNavigationUiEnabled(NavModule.getInstance().getNavigator() != null);
85-
86-
getMapAsync(
87-
new OnMapReadyCallback() {
88-
public void onMapReady(GoogleMap googleMap) {
89-
mGoogleMap = googleMap;
90-
91-
mMapViewController = new MapViewController();
92-
mMapViewController.initialize(googleMap, () -> requireActivity());
93-
94-
// Setup map listeners with the provided callback
95-
mMapViewController.setupMapListeners(NavViewFragment.this);
96-
97-
emitEvent("onMapReady", null);
98-
99-
setNavigationUiEnabled(NavModule.getInstance().getNavigator() != null);
100-
addOnRecenterButtonClickedListener(onRecenterButtonClickedListener);
101-
addPromptVisibilityChangedListener(onPromptVisibilityChangedListener);
102-
}
103-
});
104-
}
105-
106115
public MapViewController getMapController() {
107116
return mMapViewController;
108117
}

android/src/main/java/com/google/android/react/navsdk/NavViewLayout.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)