2009-11-20 29 views

Trả lời

3

Bạn đang sử dụng MapActivity? Dưới đây là đoạn code tôi đã sử dụng:

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; 

Bạn cần phải thêm mã tương tự như sau thêm:

@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; 
} 
+0

điều này xử lý các "quăng", nơi bản đồ tiếp tục di chuyển một cách ít nếu bạn ném nó (sau khi bạn nhấc ngón tay lên) –

+0

Hi cảm ơn cho marcc trả lời, nhưng tôi cần tìm một cách để phát hiện khi hoạt ảnh pan dừng lại để Tôi có thể sử dụng mã tính toán trung tâm của bạn tại thời điểm đó. – vamsibm

12

Tôi cũng đã được tìm kiếm một "đã làm cuối kéo" Giải pháp phát hiện bản đồ trung tâm tại thời điểm này chính xác sau khi bản đồ kết thúc chuyển động. Tôi đã không tìm thấy nó, vì vậy tôi đã thực hiện triển khai đơn giản này đã hoạt động tốt:

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() (e.g. call onEndDrag method) 
      this.isFirstComputeScroll = false; 
     } 
     else 
      this.lastMapCenter = this.getMapCenter(); 
    } 
} 

Đó là, tôi hy vọng điều đó sẽ hữu ích! o/

+0

Điều này đã hiệu quả, đây phải là câu trả lời được chấp nhận. – Federico

0

Kể từ tháng tám năm 2016 Maps dành cho Android là khả năng phát hiện các sự kiện như onCameraMoveStarted (và lý do cho sự di chuyển, ví dụ như, REASON_GESTURE, REASON_DEVELOPER_ANIMATION.

Các mã sau (chủ yếu là lấy từ docs) đưa ra một ý tưởng về những gì bạn có thể đạt được:?.

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; 
    } 
} 
Các vấn đề liên quan