android地图:如何在拖动完成后确定地图中心

有没有办法通过android地图API,我可以在pananimation完成后检测地图中心? 我想用这个信息来dynamic地从服务器加载标记。 谢谢BD

Solutions Collecting From Web of "android地图:如何在拖动完成后确定地图中心"

我也一直在寻找一个“完成拖动”的解决scheme,在地图结束移动的那一刻,检测地图中心。 我还没有find它,所以我做了这个简单的实现工作正常:

private class MyMapView extends MapView { private GeoPoint lastMapCenter; private boolean isTouchEnded; private boolean isFirstComputeScroll; public MyMapView(Context context, String apiKey) { super(context, apiKey); this.lastMapCenter = new GeoPoint(0, 0); this.isTouchEnded = false; this.isFirstComputeScroll = true; } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) this.isTouchEnded = false; else if (event.getAction() == MotionEvent.ACTION_UP) this.isTouchEnded = true; else if (event.getAction() == MotionEvent.ACTION_MOVE) this.isFirstComputeScroll = true; return super.onTouchEvent(event); } @Override public void computeScroll() { super.computeScroll(); if (this.isTouchEnded && this.lastMapCenter.equals(this.getMapCenter()) && this.isFirstComputeScroll) { // here you use this.getMapCenter() (eg call onEndDrag method) this.isFirstComputeScroll = false; } else this.lastMapCenter = this.getMapCenter(); } } 

就是这样,我希望它有帮助! O /

你在使用MapActivity吗? 这是我用过的代码:

 MapView mapView = (MapView)findViewById(R.id.map); Projection projection = mapView.getProjection(); int y = mapView.getHeight() / 2; int x = mapView.getWidth() / 2; GeoPoint geoPoint = projection.fromPixels(x, y); double centerLatitude = (double)geoPoint.getLatitudeE6() / (double)1E6; double centerLongitude = (double)geoPoint.getLongitudeE6() / (double)1E6; 

您确实需要添加类似于此的代码:

 @Override public boolean dispatchTouchEvent(MotionEvent event) { boolean result = super.dispatchTouchEvent(event); if (event.getAction() == MotionEvent.ACTION_UP) reload_map_data(); /// call the first block of code here return result; } 

自2016年8月起,Google Maps for Android能够检测诸如onCameraMoveStarted (以及移动的原因,例如REASON_GESTUREREASON_DEVELOPER_ANIMATION

下面的代码(大部分来自文档 )给出了你可以实现的function:

 public class MyCameraActivity extends FragmentActivity implements OnCameraMoveStartedListener, OnCameraMoveListener, OnCameraMoveCanceledListener, OnCameraIdleListener, OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_camera); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap map) { mMap = map; mMap.setOnCameraIdleListener(this); mMap.setOnCameraMoveStartedListener(this); mMap.setOnCameraMoveListener(this); mMap.setOnCameraMoveCanceledListener(this); // Show Sydney on the map. mMap.moveCamera(CameraUpdateFactory .newLatLngZoom(new LatLng(-33.87365, 151.20689), 10)); } @Override public void onCameraMoveStarted(int reason) { if (reason == OnCameraMoveStartedListener.REASON_GESTURE) { Toast.makeText(this, "The user gestured on the map.", Toast.LENGTH_SHORT).show(); } else if (reason == OnCameraMoveStartedListener .REASON_API_ANIMATION) { Toast.makeText(this, "The user tapped something on the map.", Toast.LENGTH_SHORT).show(); } else if (reason == OnCameraMoveStartedListener .REASON_DEVELOPER_ANIMATION) { Toast.makeText(this, "The app moved the camera.", Toast.LENGTH_SHORT).show(); } } @Override public void onCameraMove() { Toast.makeText(this, "The camera is moving.", Toast.LENGTH_SHORT).show(); } @Override public void onCameraMoveCanceled() { Toast.makeText(this, "Camera movement canceled.", Toast.LENGTH_SHORT).show(); } @Override public void onCameraIdle() { Toast.makeText(this, "The camera has stopped moving.", Toast.LENGTH_SHORT).show(); // Here you get the camera center GeoPoint geoPoint = projection.fromPixels(mMap.getHeight() / 2, mMap.getWidth() / 2); double centerLat = (double)geoPoint.getLatitudeE6() / (double)1E6; double centerLng = (double)geoPoint.getLongitudeE6() / (double)1E6; } }