On zoom event for google maps on android

前端 未结 11 2045
清酒与你
清酒与你 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: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
            }
    

提交回复
热议问题