标记旋转后的Android Google Map InfoWindow锚点 [英] Android Google Map InfoWindow anchor point after marker rotation

查看:25
本文介绍了标记旋转后的Android Google Map InfoWindow锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在标记旋转到始终在顶部中间后重置标记的 InfoWindows 锚点?问题是锚点随着标记旋转.

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);

说明:

如果我们假设地图标记是圆形(最合理的旋转目的),并且我们知道 InfoWindow 锚点 (B) 可以设置为从 0.0,0.0(左上角)到 1,1 的任何相对坐标点(右下)我们可以使用 SIN 和 COS 公式找到给定旋转度的圆线上的任何点.

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.

A 和 B 之间的 X 距离 = 半径 * SIN(度);A 和 B 之间的 Y 距离 = Radius * COS(degree);

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

将它们用于 Android 标记坐标,我们得到:

Adopting them for Android marker coordinates we get:

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

  1. 我们从反向旋转角度(负值)转换为正弦到弧度(度 * PI/180);
  2. 乘以圆半径(0.5)得到X轴上的距离;
  3. 向右移动半径 (+0.5) 到形状的中间(在 X 轴上);

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

  1. 从对角旋转角度(负值)求余弦转换为弧度(度 * PI/180);
  2. 乘以圆半径(0.5)得到Y轴上的距离;
  3. 按半径(-0.5)向上移动到形状的顶部(在 Y 轴上);
  4. 使值为正(带 - 符号),因为标记坐标系在 Y 轴向下具有正值;

这篇关于标记旋转后的Android Google Map InfoWindow锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆