是否可以使用Maps v2的默认ZoomControl来放大/缩小自定义量的ZoomLevel,如0.5x?

时间:2016-04-01 11:48:41

标签: android google-maps-android-api-2

我有像这样的mapFragment

   <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="5"
        tools:context="com....."/> 

我启用了默认的ZoomControle(&#34; +&#34;和&#34; - &#34;按钮):

public void onMapReady(GoogleMap googleMap){
    googleMap.getUiSettings().setZoomControlsEnabled(true);
    ...
}

使用这些ZoomControls,用户可以放大/缩小1个Zoomlevel。使用ZoomGestures,用户可以放大/缩小&#34;连续地&#34;即缩放级别之间的值。我是否可以更改地图的ZoomControls的放大/缩小行为以及与&#34; 1&#34;不同的ZoomLevel数量。例如0.5?

1 个答案:

答案 0 :(得分:0)

好的..答案是:是的! 下面是我如何将ZoomControls的行为更改为按住+单击事件并在按下并放大/缩放时同时更新UI(地图)。这种方法还可以让我轻松地为mapFragment设置MaxZoom和MinZoom等级,并且比在OnCameraChangeListener中设置ZoomLevel更好的用户体验!

我已将OnTouchListener设置为&#34; +&#34;和&#34; - &#34;来自mapFragment的孩子。当手指触摸&#34; MotionEvent.ACTION_DOWN&#34;时,会启动用于放大/缩小地图的线程。在&#34; MotionEvent.ACTION_UP&#34;上,线程被中断:

    mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    // get the default ZoomControls from the mapFragment:
    View zoomControls = mapFragment.getView().findViewById(0x1);

    for(int i=0;i<((ViewGroup)zoomControls).getChildCount();i++){
        View child=((ViewGroup)zoomControls).getChildAt(i);
        if (i==0) {
            // there is your "+" button, zoom in
            child.setOnTouchListener(new View.OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            updateZoom = new ZoomInMapThread();
                            updateZoom.start();
                            break;
                        case MotionEvent.ACTION_UP:
                            updateZoom.interrupt();
                            break;
                    }
                    return true;
                }
            });
        }
        if (i==1) {
            // there is your "-" button, zoom out
            child.setOnTouchListener(new View.OnTouchListener(){
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            updateZoom = new ZoomOutMapThread();
                            updateZoom.start();
                            break;
                        case MotionEvent.ACTION_UP:
                            updateZoom.interrupt();
                            break;
                    }
                    return true;
                }
            });
        }
    }

在Activity类的最后,我创建了ZoomThreads:

private class ZoomOutMapThread extends Thread {
    @Override
    public void run(){
        try {
            while (!Thread.currentThread().isInterrupted()){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        currentZoomLevel = currentZoomLevel - 0.1f;
                        if (currentZoomLevel < minZoom){
                            currentZoomLevel = minZoom;
                        }
                        mMap.animateCamera(CameraUpdateFactory.zoomTo(currentZoomLevel));
                        Log.d("Zoom", "Level:" + currentZoomLevel);
                    }
                });
                Thread.sleep(100);
            }
        } catch (InterruptedException consumed) {
            consumed.printStackTrace();
        }
    }
}

private class ZoomInMapThread extends Thread {
    @Override
    public void run(){
        try {
            while (!Thread.currentThread().isInterrupted()){
                // make the Thread able to update the UserInterace:
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // increase the currentZoomLevel by 0.1:
                        currentZoomLevel = currentZoomLevel + 0.1f;
                        // increase only while we have not reached predefined maxZoom:
                        if (currentZoomLevel > maxZoom){
                            currentZoomLevel = maxZoom;
                            // good moment to disable the "+" button somehow
                        }
                        // update the Camera in GoogleMap mMap from onMapReady():  mMap.animateCamera(CameraUpdateFactory.zoomTo(currentZoomLevel));
                        Log.d("Zoom","Level:"+currentZoomLevel);
                    }
                });
                // repeat every 100ms while finger holds the "+"-Button
                Thread.sleep(100);
            }
        } catch (InterruptedException consumed) {
            consumed.printStackTrace();
        }
    }
}

别忘了声明类变量:

private float maxZoom = 9.99f;
private float minZoom = 8.2f;
private float currentZoomLevel = 9.1f;

和forMapReady(GoogleMap ..):

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mMap.getUiSettings().setRotateGesturesEnabled(false);   // prevent rotation UI
    mMap.getUiSettings().setMapToolbarEnabled(false);       // prevent navigate to UI
    mMap.getUiSettings().setZoomGesturesEnabled(false);     // prevent default Zoom Gestures
    mMap.getUiSettings().setZoomControlsEnabled(true);      // show default Zoom Controls (+/-)