How to reset marker's InfoWindows anchor point after marker has been rotated to be always in top middle? The problem is that the anchor point is rotated along with marker.
static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker marker = mMap.addMarker(new MarkerOptions()
.position(PERTH)
.anchor(0.5,0.5)
.rotation(90.0)
.infoWindowAnchor(0.5,0));
//Update marker with new data (position and direction angle)
var angle = 130.0;
marker.setPosition(new LatLng(-30.20, 113.27));
marker.setRotation(angle);
marker.setInfoWindowAnchor(x,y); // how to calculate these values?
var angle = 130.0; // rotation angle
var x = Math.sin(-angle * Math.PI / 180) * 0.5 + 0.5;
var y = -(Math.cos(-angle * Math.PI / 180) * 0.5 - 0.5);
marker.setInfoWindowAnchor((float)x, (float)y);
Explanation:
If we assume that map marker is circular shape (most reasonable for rotation purpose) and as we know that InfoWindow anchor point (B) can be set to any relative coordinate point from 0.0,0.0 (upper left) to 1,1 (lower right) we can find any point on circle line by given rotation degree using SIN and COS formulas.
X distance between A and B = Radius * SIN(degree); Y distance between A and B = Radius * COS(degree);
Adopting them for Android marker coordinates we get:
var x = Math.sin(-angle * Math.PI / 180) * 0.5 + 0.5;
var y = -(Math.cos(-angle * Math.PI / 180) * 0.5 - 0.5);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With