dapriett / nativescript-google-maps-sdk

Cross Platform Google Maps SDK for Nativescript
MIT License
244 stars 164 forks source link

How to set position of InfoWindow when marker is centered on location #435

Closed dlcole closed 3 years ago

dlcole commented 3 years ago

I have a long arrow marker on a map centered on a point, and then rotated to match the wind direction. To center the marker I set marker.anchor = [0.5, 0.5]; This works fine, but when I open an InfoWindow to provide information pertinent to that point, the InfoWindow always points to the head of the arrow, not the marker's anchor point.

I tried setting the anchor to [0.5, 0], displaying the InfoWindow, and setting the anchor back to [0.5, 0.5]. This almost works, but the InfoWindow moves with the arrow when I change the anchor, so the end result is the same.

On iOS I've found marker._ios.infoWindowAnchor with a value of [0.5, 0] which matches the behavior I see, but setting that directly does not have any effect.

Is there any way to control what the InfoWindow points to?

Or, to ask another way, is there any way to access the InfoWindow object?

dlcole commented 3 years ago

On Android, this code works:

marker.android.setInfoWindowAnchor(0.5, 0.5);

Now continuing my search on iOS.

What I've found so far...

iOS does not have a specific setter function for infoWindowAnchor, but there is the marker.ios.setValueForKey function that appears to let you specify the name of the property to be set and its value. I'm experimenting with the syntax and have tried different variations, such as

marker.ios.setValueForKey([0.5, 0.5], "infoWindowAnchor" );

and

marker.ios.setValueForKey({x: 0.5, y: 0.5}, "infoWindowAnchor" );

both of which crash immediately with NativeScript caught signal 11, even within a debugger and within a Try / Catch block.

This code: marker.ios.setValueForKey((0.5, 0.5), "infoWindowAnchor" ); executes but ends up setting both x and y to 0 rather that .5. At least that's some progress.

I'll continue tomorrow...

nickolanack commented 3 years ago

have you tried like this:? marker.ios.infoWindowAnchor=CGPointMake(0.5, 0.5);

dlcole commented 3 years ago

@nickolanack - that works!

It occurred to me last night while I should have been sleeping that I probably needed to create the point object and assign that. Using setValueForKey crashed immediately, though, but assigning the point directly, as you suggest, works just fine.

For folks following along at home, here's the final code:

// Set anchor so infoWindow points to center of marker 
if (marker.android) {
  marker.android.setInfoWindowAnchor(0.5, 0.5);
} else {
  marker._ios.infoWindowAnchor = CGPointMake(0.5, 0.5);
}

Thanks!

dlcole commented 3 years ago

All of a sudden the Android code, above, is failing. I have found that the following code works:

marker.android.infoWindowAnchor(0.5, 0.5);