Skip to content

Commit 4372c96

Browse files
feat: add marker rotation anchor support (#608)
1 parent 062d65b commit 4372c96

10 files changed

Lines changed: 50 additions & 0 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ private Marker createMarker(Map<String, Object> optionsMap, String customId) {
259259
boolean draggable = CollectionUtil.getBool("draggable", optionsMap, false);
260260
boolean flat = CollectionUtil.getBool("flat", optionsMap, false);
261261
boolean visible = CollectionUtil.getBool("visible", optionsMap, true);
262+
float anchorU = 0.5f;
263+
float anchorV = 1.0f;
264+
if (optionsMap.containsKey("anchor")) {
265+
Map<String, Object> anchor = (Map<String, Object>) optionsMap.get("anchor");
266+
if (anchor != null) {
267+
anchorU = Double.valueOf(CollectionUtil.getDouble("u", anchor, 0.5)).floatValue();
268+
anchorV = Double.valueOf(CollectionUtil.getDouble("v", anchor, 1.0)).floatValue();
269+
}
270+
}
262271

263272
MarkerOptions options = new MarkerOptions();
264273
if (imagePath != null && !imagePath.isEmpty()) {
@@ -284,6 +293,7 @@ private Marker createMarker(Map<String, Object> optionsMap, String customId) {
284293
options.flat(flat);
285294
options.alpha(alpha);
286295
options.rotation(rotation);
296+
options.anchor(anchorU, anchorV);
287297
options.draggable(draggable);
288298
options.visible(visible);
289299

@@ -307,6 +317,15 @@ private void updateMarker(Marker marker, Map<String, Object> optionsMap) {
307317
boolean draggable = CollectionUtil.getBool("draggable", optionsMap, false);
308318
boolean flat = CollectionUtil.getBool("flat", optionsMap, false);
309319
boolean visible = CollectionUtil.getBool("visible", optionsMap, true);
320+
float anchorU = 0.5f;
321+
float anchorV = 1.0f;
322+
if (optionsMap.containsKey("anchor")) {
323+
Map<String, Object> anchor = (Map<String, Object>) optionsMap.get("anchor");
324+
if (anchor != null) {
325+
anchorU = Double.valueOf(CollectionUtil.getDouble("u", anchor, 0.5)).floatValue();
326+
anchorV = Double.valueOf(CollectionUtil.getDouble("v", anchor, 1.0)).floatValue();
327+
}
328+
}
310329

311330
if (imagePath != null && !imagePath.isEmpty()) {
312331
try {
@@ -331,6 +350,7 @@ private void updateMarker(Marker marker, Map<String, Object> optionsMap) {
331350
marker.setFlat(flat);
332351
marker.setAlpha(alpha);
333352
marker.setRotation(rotation);
353+
marker.setAnchor(anchorU, anchorV);
334354
marker.setDraggable(draggable);
335355
marker.setVisible(visible);
336356
}

example/src/screens/integration_tests/integration_test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ export const testMapMarkers = async (testTools: TestTools) => {
921921
position: { lat: 37.7849, lng: -122.4094 },
922922
title: 'Marker with Icon',
923923
imgPath: 'circle.png',
924+
anchor: { u: 0.5, v: 0.5 },
924925
});
925926

926927
if (!markerWithIcon.id) {

ios/react-native-navigation-sdk/NavAutoModule.mm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,22 @@ - (void)addMarker:(MarkerOptionsSpec &)options
152152
}
153153
}
154154

155+
CGFloat anchorU = 0.5;
156+
CGFloat anchorV = 1.0;
157+
if (optionsCopy.anchor().has_value()) {
158+
auto anchor = optionsCopy.anchor().value();
159+
anchorU = anchor.u();
160+
anchorV = anchor.v();
161+
}
162+
155163
GMSMarker *marker = [ObjectTranslationUtil
156164
createMarker:position
157165
title:optionsCopy.title()
158166
snippet:optionsCopy.snippet()
159167
alpha:(float)optionsCopy.alpha().value_or(1.0)
160168
rotation:optionsCopy.rotation().value_or(0.0)
161169
flat:optionsCopy.flat().value_or(NO)
170+
anchor:CGPointMake(anchorU, anchorV)
162171
draggable:optionsCopy.draggable().value_or(NO)
163172
icon:icon
164173
zIndex:optionsCopy.zIndex().has_value() ? @(optionsCopy.zIndex().value()) : nil

ios/react-native-navigation-sdk/NavViewController.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ - (void)addMarker:(GMSMarker *)marker
756756
alpha:marker.opacity
757757
rotation:marker.rotation
758758
flat:marker.flat
759+
anchor:marker.groundAnchor
759760
draggable:marker.draggable
760761
icon:marker.icon
761762
zIndex:@(marker.zIndex)

ios/react-native-navigation-sdk/NavViewModule.mm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,22 @@ - (void)addMarker:(NSString *)nativeID
155155
}
156156
}
157157

158+
CGFloat anchorU = 0.5;
159+
CGFloat anchorV = 1.0;
160+
if (optionsCopy.anchor().has_value()) {
161+
auto anchor = optionsCopy.anchor().value();
162+
anchorU = anchor.u();
163+
anchorV = anchor.v();
164+
}
165+
158166
GMSMarker *marker = [ObjectTranslationUtil
159167
createMarker:position
160168
title:optionsCopy.title()
161169
snippet:optionsCopy.snippet()
162170
alpha:(float)optionsCopy.alpha().value_or(1.0)
163171
rotation:optionsCopy.rotation().value_or(0.0)
164172
flat:optionsCopy.flat().value_or(NO)
173+
anchor:CGPointMake(anchorU, anchorV)
165174
draggable:optionsCopy.draggable().value_or(NO)
166175
icon:icon
167176
zIndex:optionsCopy.zIndex().has_value() ? @(optionsCopy.zIndex().value()) : nil

ios/react-native-navigation-sdk/ObjectTranslationUtil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
alpha:(float)alpha
4343
rotation:(double)rotation
4444
flat:(BOOL)flat
45+
anchor:(CGPoint)anchor
4546
draggable:(BOOL)draggable
4647
icon:(nullable UIImage *)icon
4748
zIndex:(nullable NSNumber *)zIndex
@@ -101,6 +102,7 @@
101102
alpha:(float)alpha
102103
rotation:(double)rotation
103104
flat:(BOOL)flat
105+
anchor:(CGPoint)anchor
104106
draggable:(BOOL)draggable
105107
icon:(nullable UIImage *)icon
106108
zIndex:(nullable NSNumber *)zIndex

ios/react-native-navigation-sdk/ObjectTranslationUtil.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ + (GMSMarker *)createMarker:(CLLocationCoordinate2D)position
275275
alpha:(float)alpha
276276
rotation:(double)rotation
277277
flat:(BOOL)flat
278+
anchor:(CGPoint)anchor
278279
draggable:(BOOL)draggable
279280
icon:(nullable UIImage *)icon
280281
zIndex:(nullable NSNumber *)zIndex
@@ -285,6 +286,7 @@ + (GMSMarker *)createMarker:(CLLocationCoordinate2D)position
285286
marker.opacity = alpha;
286287
marker.rotation = rotation;
287288
marker.flat = flat;
289+
marker.groundAnchor = anchor;
288290
marker.draggable = draggable;
289291
if (icon) {
290292
marker.icon = icon;
@@ -431,6 +433,7 @@ + (void)updateMarker:(GMSMarker *)marker
431433
alpha:(float)alpha
432434
rotation:(double)rotation
433435
flat:(BOOL)flat
436+
anchor:(CGPoint)anchor
434437
draggable:(BOOL)draggable
435438
icon:(nullable UIImage *)icon
436439
zIndex:(nullable NSNumber *)zIndex
@@ -441,6 +444,7 @@ + (void)updateMarker:(GMSMarker *)marker
441444
marker.opacity = alpha;
442445
marker.rotation = rotation;
443446
marker.flat = flat;
447+
marker.groundAnchor = anchor;
444448
marker.draggable = draggable;
445449
if (icon) {
446450
marker.icon = icon;

src/maps/mapView/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export interface MarkerOptions {
7070
draggable?: boolean;
7171
/** Indicates whether this marker should be flat against the map true or a billboard facing the camera false. */
7272
flat?: boolean;
73+
/** The anchor point of the marker image in normalized coordinates (0-1). Use `{ u: 0.5, v: 0.5 }` to rotate around the image center. Platform default is bottom-center `{ u: 0.5, v: 1.0 }`. */
74+
anchor?: { u: number; v: number };
7375
/** Indicates the visibility of the polygon. True by default. */
7476
visible?: boolean;
7577
}

src/native/NativeNavAutoModule.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type MarkerOptionsSpec = Readonly<{
5656
rotation?: WithDefault<Float, 0>;
5757
draggable?: WithDefault<boolean, false>;
5858
flat?: WithDefault<boolean, false>;
59+
anchor?: Readonly<{ u: Float; v: Float }>;
5960
visible?: WithDefault<boolean, true>;
6061
zIndex?: WithDefault<Double, null>;
6162
}>;

src/native/NativeNavViewModule.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type CameraPositionSpec = Readonly<{
4747

4848
type MarkerOptionsSpec = Readonly<{
4949
alpha?: WithDefault<Float, 0>;
50+
anchor?: Readonly<{ u: Float; v: Float }>;
5051
draggable?: WithDefault<boolean, false>;
5152
flat?: WithDefault<boolean, false>;
5253
id?: WithDefault<string, null>;

0 commit comments

Comments
 (0)