在Android Mapview中获取地图的中心坐标

在Android Mapview中获取地图的中心坐标,android,Android,在我的应用程序中,当用户在地图上滚动时,我试图获得地图的中心坐标 我想得到坐标并在文本视图上设置它 以下是我的代码: public boolean onTouchEvent(MotionEvent event) { int action=event.getAction(); projection=mapView.getProjection(); int X = (int)event.getX(); int Y =

在我的应用程序中,当用户在地图上滚动时,我试图获得地图的中心坐标

我想得到坐标并在文本视图上设置它

以下是我的代码:

public boolean onTouchEvent(MotionEvent event) {
        int action=event.getAction();
        projection=mapView.getProjection();
        int X = (int)event.getX();          
        int Y = (int)event.getY();
        if(action==MotionEvent.ACTION_MOVE)
        {


            metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

            GeoPoint G = projection.fromPixels(metrics.heightPixels/2, metrics.widthPixels/2);
        //GeoPoint p=   mapView.getMapCenter();
            int lati=p.getLatitudeE6();
            Log.i("Lat : ",""+lati);
            Toast.makeText(this,""+lati,Toast.LENGTH_LONG);

            int longi=p.getLongitudeE6();
            Log.i("Lon : ",""+longi);
            Toast.makeText(this,""+longi,Toast.LENGTH_LONG);
            lat.setText(""+lati);
            lon.setText(""+longi);
        }
        return true;
    }
使用本文:

它将回答您关于使用mapview的所有问题。(主要是所有问题)。此外,您还需要:


在页面上搜索“获取被触摸的位置”

OP在他的问题中包含了正确的答案。这是如何做到的:

GeoPoint mapCenter = mapView.getProjection().fromPixels(
        mapView.getWidth()/2,
        mapView.getHeight()/2);

int lat = mapCenter.getLatitudeE6();
int lon = mapCenter.getLongitudeE6();

API略有更新。以下是实际版本:

val mapViewCenter = Point(
    googleMapView.width / 2,
    googleMapView.height / 2
)
val mapCenter: LatLng = googleMap.projection.fromScreenLocation(mapViewCenter)

你能解释一下什么不起作用吗?你知道怎么做吗?我正在努力实现同样的目标。Thanks@Thiago问题已更新为答案。与我使用的类似:projection=mapView.getProjection();GeoPoint CoordinatesOffmCenter=projection.fromPixels(mapView.getWidth()/2,mapView.getHeight()/2);