Java 关于 android 上谷歌地图的缩放事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2013443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:32:11  来源:igfitidea点击:

On zoom event for google maps on android

javaandroideventsgoogle-mapszoom

提问by Bjarke Freund-Hansen

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

我们正在构建一个使用谷歌地图 api for android 的应用程序。

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

我有我的 MapController 和 MapView,我使用以下方法启用内置缩放控件:

mapView.setBuiltInZoomControls(true);

I would now like to get an event when the user actually zooms on the map, how do I go about that? I can find no such event or any general event where I could detect a change in zoom level.

我现在想在用户实际放大地图时获得一个事件,我该怎么做?我找不到可以检测到缩放级别变化的此类事件或任何一般事件。

Update

更新

The mapView.getZoomControls() is deprecated. And the documentation suggests using mapView.setBuiltInZoomControls(bool) instead. This is okay, but I simply cannot figure out how to act on events from the built in zoom controls.

mapView.getZoomControls() 已弃用。并且文档建议使用 mapView.setBuiltInZoomControls(bool) 代替。这没关系,但我根本无法弄清楚如何对内置缩放控件中的事件采取行动。

采纳答案by tarmelop

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

使用 Google Maps Android API v2,您可以像这样使用GoogleMap.OnCameraChangeListener

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
        }
    }
});

回答by JasonOfEarth

The android APIsuggests you use ZoomControlswhich has a setOnZoomInClickListener()

Android的API建议您使用ZoomControls具有setOnZoomInClickListener()

To add these zoom controls you would:

要添加这些缩放控件,您需要:

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

And then add mZoom to your layout.

然后将 mZoom 添加到您的布局中。

回答by Billy Bob Bain

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

正如 OP 所说,使用 onUserInteractionEvent 并自己进行测试。

回答by Felipe Conde

You Can use

您可以使用

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

hope it helps

希望能帮助到你

回答by Rakesh Gondaliya

U can have Zoom CControl like this.

你可以像这样拥有Zoom CControl。

Here i am attaching code how u can implement zoomcontrol.

在这里,我附上了如何实现缩放控制的代码。

1>> XML file should be like this.

1>> XML 文件应该是这样的。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <com.google.android.maps.MapView 
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="0l4sCTTyRmXTNo7k8DREHvEaLar2UmHGwnhZVHQ"
        />

    <LinearLayout android:id="@+id/zoom" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_centerHorizontal="true" 
        /> 

</RelativeLayout>

2>> On you main activity inside oncreate method do following things. here you are inflating your view with mapView

2>> oncreate 方法中的主要活动执行以下操作。在这里,您正在使用 mapView 扩展您的视图

mapView = (MapView) findViewById(R.id.mapView);
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
        View zoomView = mapView.getZoomControls(); 

        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);

........................................................... This is how you can use inbuilt zoom control of API..

……………………………………………………………………………………………………………………………………………………………… .........这是您如何使用 API 的内置缩放控制..

You can have your own custom code to implement Zoom in And Zoom out like this...

您可以拥有自己的自定义代码来实现像这样放大和缩小...

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    MapController mc = mapView.getController(); 
    switch (keyCode) 
    {
        case KeyEvent.KEYCODE_3:
            mc.zoomIn();
            break;
        case KeyEvent.KEYCODE_1:
            mc.zoomOut();
            break;
    }
    return super.onKeyDown(keyCode, event);
}    

..This is how i have implemented...

..这就是我实施的方式...

Thanks..Rakesh

谢谢..Rakesh

回答by Abhinav

I use this library which has an onZoom function listener. http://code.google.com/p/mapview-overlay-manager/. Works well.

我使用这个库,它有一个 onZoom 函数侦听器。http://code.google.com/p/mapview-overlay-manager/。效果很好。

回答by Kurru

You can implement your own basic "polling" of the zoom value to detect when the zoom has been changed using the android Handler.

您可以实现自己的缩放值的基本“轮询”,以使用 android Handler检测缩放何时更改。

Using the following code for your runnable event. This is where your processing should be done.

将以下代码用于您的可运行事件。这是您应该完成处理的地方。

private Handler handler = new Handler();

public static final int zoomCheckingDelay = 500; // in ms

private Runnable zoomChecker = new Runnable()
{
    public void run()
    {
        checkMapIcons();

        handler.removeCallbacks(zoomChecker); // remove the old callback
        handler.postDelayed(zoomChecker, zoomCheckingDelay); // register a new one
    }
};

Starta callback event using

使用启动回调事件

protected void onResume()
{
    super.onResume();
    handler.postDelayed(zoomChecker, zoomCheckingDelay);
}

and stopit when you're leaving the activity using

并在您离开活动时停止使用

protected void onPause()
{
    super.onPause();
    handler.removeCallbacks(zoomChecker); // stop the map from updating
}

Articlethis is based on can be found hereif you want a longer write up.

如果你想写更长的文章,可以在这里找到这篇文章

回答by jiduvah

I used a mixture of the above. I found that using the timmer to start a thread every half a second cause the map to be really jenky. Probably because I was using a couple of If statments everytime. So I started the thread on a post delay of 500ms from the onUserInteraction. This gives enough time for the zoomLevel to update before the thread starts to run thus getting the correct zoomlevel without running a thread every 500ms.

我使用了上述的混合物。我发现使用定时器每半秒启动一个线程会导致地图变得非常笨拙。可能是因为我每次都使用几个 If 语句。所以我在从 onUserInteraction 延迟 500 毫秒后启动了线程。这为在线程开始运行之前更新 zoomLevel 提供了足够的时间,从而获得正确的缩放级别,而无需每 500 毫秒运行一次线程。

public void onUserInteraction() {
    handler.postDelayed(zoomChecker, zoomCheckingDelay);
}

回答by Paul

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?

我认为这个问题可能还有另一个答案。Overlay 类绘制方法在任何缩放后调用,我相信这是在缩放级别更改后调用的。你不能设计你的应用程序来利用这一点吗?如果您愿意,您甚至可以使用虚拟覆盖层来检测这一点。这不会比使用具有延迟的可运行更有效吗?

@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);
}

回答by Damian

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).

这是一个干净的解决方案。只需将此 TouchOverlay 私有类添加到您的活动中,并添加一个名为 onZoom 的方法(由该内部类调用)。

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

请注意,您必须将此 TouchOverlay 添加到您的地图视图中,例如

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.

每当用户触摸地图时,它都会跟踪缩放级别,例如双击或双指缩放,然后在缩放级别更改时触发 onZoom 方法(使用缩放级别)。

   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
        }