On zoom event for google maps on android

前端 未结 11 1874
清酒与你
清酒与你 2020-11-30 00:04

We\'re building an application which is using the google maps api for android.

I have my MapController and MapView, and I enable the built-in zoom controls using:

相关标签:
11条回答
  • 2020-11-30 00:13

    I think there might another answer to this question. The Overlay class draw method is called after any zoom and I believe this is called after the zoom level changes. Could you not architect your app to take advantage of this? You could even use a dummy overlay just to detect this if you wanted to. Would this not be more efficient than using a runnable with a delay?

    @Override
    public boolean draw (Canvas canvas, MapView mapView, boolean shadow, long when) {
        int zoomLevel = mapView.getZoomLevel();
        // do what you want with the zoom level
        return super.draw(canvas, mapView, shadow, when);
    }
    
    0 讨论(0)
  • 2020-11-30 00:20

    With the Google Maps Android API v2 you can use a GoogleMap.OnCameraChangeListener like this:

    mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
    
        private float currentZoom = -1;
    
        @Override
        public void onCameraChange(CameraPosition pos) {
            if (pos.zoom != currentZoom){
                currentZoom = pos.zoom;
                // do you action here
            }
        }
    });
    
    0 讨论(0)
  • 2020-11-30 00:21

    Here is a clean solution. Simply add this TouchOverlay private class to your activity and a method called onZoom (that is called by this inner class).

    Note, you'll have to add this TouchOverlay to your mapView e.g.

    mapView.getOverlays().add(new TouchOverlay());
    

    It keeps track of the zoom level whenever the user touches the map e.g. to double-tap or pinch zoom and then fires the onZoom method (with the zoom level) if the zoom level changes.

       private class TouchOverlay extends com.google.android.maps.Overlay {
                int lastZoomLevel = -1;
    
                @Override
                public boolean onTouchEvent(MotionEvent event, MapView mapview) {
                    if (event.getAction() == 1) {
                        if (lastZoomLevel == -1)
                            lastZoomLevel = mapView.getZoomLevel();
    
                        if (mapView.getZoomLevel() != lastZoomLevel) {
                            onZoom(mapView.getZoomLevel());
                            lastZoomLevel = mapView.getZoomLevel();
                        }
                    }
                    return false;
                }
            }
    
            public void onZoom(int level) {
                reloadMapData(); //act on zoom level change event here
            }
    
    0 讨论(0)
  • 2020-11-30 00:21

    The android API suggests you use ZoomControls which has a setOnZoomInClickListener()

    To add these zoom controls you would:

    ZoomControls mZoom = (ZoomControls) mapView.getZoomControls();
    

    And then add mZoom to your layout.

    0 讨论(0)
  • 2020-11-30 00:23

    You Can use

    ZoomButtonsController zoomButton = mapView.getZoomButtonsController();
    zoomButton.setOnZoomListener(listener);
    

    hope it helps

    0 讨论(0)
  • 2020-11-30 00:26

    As the OP said, use the onUserInteractionEvent and do the test yourself.

    0 讨论(0)
提交回复
热议问题