最近项目在搞一个需求- 实现滑动地图实时刷新屏幕中心icon的附近数据.这种需求较为常见,ofo以及摩拜单车等都采用该方式获取屏幕中心的附近车辆数据.
主要的实现就是将屏幕上的像素点转换为实际的经纬度坐标,核心代码如下所示:
/**
* by moos on 2017/09/05
* func:获取屏幕中心的经纬度坐标
* @return
*/
public LatLng getMapCenterPoint() {
int left = mMapView.getLeft();
int top = mMapView.getTop();
int right = mMapView.getRight();
int bottom = mMapView.getBottom();
// 获得屏幕点击的位置
int x = (int) (mMapView.getX() + (right - left) / 2);
int y = (int) (mMapView.getY() + (bottom - top) / 2);
Projection projection = mAMap.getProjection();
LatLng pt = projection.fromScreenLocation(new Point(x, y));
return pt;
}
通过此方法便可以直接获取到固定像素点的经纬度坐标了,方法直接拿来用即可.