如何从谷歌地图 v2 android 中的标记获取屏幕坐标 [英] How to get screen coordinates from marker in google maps v2 android
问题描述
我有一个小的安卓问题(谷歌地图 v2 api)
I have a small android problem (google maps v2 api)
这是我的代码:
GoogleMaps mMap;
Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(20, 20)));
我正在尝试找到一种方法来获取此标记对象的当前屏幕坐标 (x,y).
I am trying to find a way to get the current screen coordinates (x,y) for this marker object.
也许有人有想法?我试过 getProjection 但它没有看到工作.谢谢!:)
Perhaps someone has an idea? I tried getProjection but it does not see to work. Thanks! :)
推荐答案
是的,使用 Projection
类.更具体地说:
Yes, use Projection
class. More specifically:
获取地图的
Projection
:
Projection projection = map.getProjection();
获取标记的位置:
Get location of your marker:
LatLng markerLocation = marker.getPosition();
将位置传递给 Projection.toScreenLocation()
方法:
Point screenPosition = projection.toScreenLocation(markerLocation);
仅此而已.现在 screenPosition
将包含标记相对于整个 Map 容器左上角的位置:)
That's all. Now screenPosition
will contain the position of the marker relative to the top-left corner of the whole Map container :)
请记住,Projection
对象只会在地图通过布局过程后返回有效值(即它具有有效的width
和height
设置).您可能会收到 (0, 0)
因为您试图过早地访问标记的位置,就像在这种情况下一样:
Remember, that the Projection
object will only return valid values after the map has passed the layout process (i.e. it has valid width
and height
set). You're probably getting (0, 0)
because you're trying to access position of the markers too soon, like in this scenario:
- 通过扩展布局 XML 文件来创建地图
- 初始化地图.
- 向地图添加标记.
- 查询地图
Projection
在屏幕上的标记位置.
- Create the map from layout XML file by inflating it
- Initialize the map.
- Add markers to the map.
- Query
Projection
of the map for marker positions on the screen.
这不是一个好主意,因为地图没有设置有效的宽度和高度.您应该等到这些值有效.解决方案之一是将 OnGlobalLayoutListener
附加到地图视图并等待布局过程解决.在扩展布局并初始化地图之后执行此操作 - 例如在 onCreate()
中:
This is not a good idea since the the map doesn't have valid width and height set. You should wait until these values are valid. One of the solutions is attaching a OnGlobalLayoutListener
to the map view and waiting for layout process to settle. Do it after inflating the layout and initializing the map - for example in onCreate()
:
// map is the GoogleMap object
// marker is Marker object
// ! here, map.getProjection().toScreenLocation(marker.getPosition()) will return (0, 0)
// R.id.map is the ID of the MapFragment in the layout XML file
View mapView = getSupportFragmentManager().findFragmentById(R.id.map).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// remove the listener
// ! before Jelly Bean:
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// ! for Jelly Bean and later:
//mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// set map viewport
// CENTER is LatLng object with the center of the map
map.moveCamera(CameraUpdateFactory.newLatLngZoom(CENTER, 15));
// ! you can query Projection object here
Point markerScreenPosition = map.getProjection().toScreenLocation(marker.getPosition());
// ! example output in my test code: (356, 483)
System.out.println(markerScreenPosition);
}
});
}
请通读评论以获取更多信息.
Please read through the comments for additional informations.
这篇关于如何从谷歌地图 v2 android 中的标记获取屏幕坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!