如何从标记在谷歌屏幕坐标映射V2机器人坐标、机器人、标记、屏幕

2023-09-12 01:07:25 作者:可念不可说

我有一个小机器人的问题(谷歌地图V2 API)

I have a small android problem (google maps v2 api)

这是我的code:

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! :)

推荐答案

是的,使用投影类。更具体地讲:

Yes, use Projection class. More specifically:

获取投影地图:

Projection projection = map.getProjection();

让您的标记位置:

Get location of your marker:

LatLng markerLocation = marker.getPosition();

传递位置到 Projection.toScreenLocation()方法:

Point screenPosition = projection.toScreenLocation(markerLocation);

这就是全部。现在 screenPosition 将包含相对于整个地图容器的左上角的标记的位置:)

That's all. Now screenPosition will contain the position of the marker relative to the top-left corner of the whole Map container :)

记住,该投影对象将只返回有效值的之后,地图已通过布局过程的(即具有有效的宽度高度设置)。你可能会得到(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文件中的地图 初始化地图。 添加标记到地图。 查询投影地图在屏幕上标记位置。 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.

 
精彩推荐