【问题标题】:Get the center coordinates of map in Android Mapview在Android Mapview中获取地图的中心坐标
【发布时间】:2020-01-30 20:36:10
【问题描述】:

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

我想获取坐标并将其设置在文本视图上。

这是我的代码:

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

【问题讨论】:

  • 你能解释一下什么不起作用吗?

标签: android


【解决方案1】:

使用这篇文章:

http://mobiforge.com/developing/story/using-google-maps-android

它将回答您关于使用 mapview 的所有问题。(大部分)。再加上您的特殊需求:

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

【讨论】:

  • 你知道怎么做吗?我正在努力实现同样的目标。谢谢
  • @Thiago 这个问题已经用答案更新了。类似于我正在使用的:projection=mapView.getProjection(); GeoPoint坐标OfMapCenter = projection.fromPixels(mapView.getWidth()/2, mapView.getHeight()/2);
【解决方案2】:

OP 在他的问题中包含了正确答案。这是怎么做的:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 2018-12-19
      相关资源
      最近更新 更多