Google Map Android Map InfoWindow After Turning Marker

How to reset InfoWindows anchor point marker after marker has been rotated to always be in the upper middle? The problem is that the anchor point rotates with the 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? 

enter image description here

+5
source share
1 answer
 var angle = 130.0; 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 the map marker is round (the most reasonable for the purpose of rotation), and, as we know, the InfoWindow (B) anchor point can be set to any relative coordinate point from 0,0,0,0 (at the top left) to 1.1 (bottom right) we can find any point on the circle line with a given degree of rotation using the SIN and COS formulas.

enter image description here

X distance between A and B = radius * SIN (degree); Y distance between A and B = radius * COS (degree);

Accepting them for the coordinates of the Android marker, we get:

var x = Math.sin(-angle * Math.PI / 180) * 0.5 + 0.5;

  • We find SINE from the opposite rotation angle (negative value) to radians (degree * PI / 180);
  • Multiply the radius of the circle (0.5) to get the distance along the X axis;
  • Move right along the radius (+0.5) in the middle of the form (along the X axis);

var y = -(Math.cos(-angle * Math.PI / 180) * 0.5 - 0.5);

  1. Find COSINE from the rotation angle of the oposit (negative value) converted to radians (degree * PI / 180);
  2. Multiply the radius of the circle (0.5) to get the distance along the Y axis;
  3. Move up the radius (-0.5) at the top of the shape (along the Y axis);
  4. Make the value positive (with a - sign) as the marker coordinate system has positive values ​​on the Y axis down;
+10
source

Source: https://habr.com/ru/post/1232943/


All Articles