Google 地图 Android API v2 - 交互式信息窗口(就像原始 Android 谷歌地图一样)

2023-11-25

我正在尝试定制InfoWindow使用新的 Google Maps API v2 单击标记后。我希望它看起来像谷歌原始地图应用程序中的那样。像这样:

Example Image

当我有ImageButton在里面,它不起作用 - 整个InfoWindow被选中,而不仅仅是ImageButton。我读到这是因为没有View它本身但它是快照,因此无法区分各个项目。

EDIT:在里面文档(谢谢Disco S2):

正如前面关于信息窗口的部分所述,信息窗口 不是实时视图,而是将视图渲染为图像到 地图。因此,您在视图上设置的任何侦听器都将被忽略 并且您无法区分各个部分的点击事件 风景。建议您不要放置交互组件——例如 作为按钮、复选框或文本输入 - 在您的自定义信息中 窗户。

但如果谷歌使用它,就必须有某种方法来实现它。有人有什么主意吗?


我自己正在寻找解决这个问题的方法,但没有运气,所以我不得不自己动手,我想在这里与大家分享。 (请原谅我的英语不好)(用英语回答另一个捷克人有点疯狂:-))

我尝试的第一件事就是使用一个好的旧的PopupWindow。这很简单 - 只需要听OnMarkerClickListener然后显示一个自定义PopupWindow位于标记上方。 StackOverflow 上的其他一些人建议了这个解决方案,乍一看它实际上看起来相当不错。但当您开始移动地图时,此解决方案的问题就会显现出来。你必须移动PopupWindow不知何故,你自己这是可能的(通过监听一些 onTouch 事件),但恕我直言,你不能让它看起来足够好,特别是在一些慢速设备上。如果你以简单的方式做到这一点,它就会从一个地方“跳”到另一个地方。您还可以使用一些动画来完善这些跳跃,但这样PopupWindow总是会“落后一步”,而它应该在地图上,但我只是不喜欢。

此时,我正在考虑其他解决方案。我意识到我实际上并不需要那么多的自由 - 来显示我的自定义视图以及随之而来的所有可能性(例如动画进度条等)。我认为即使是谷歌工程师也不在谷歌地图应用程序中这样做是有充分理由的。我所需要的只是 InfoWindow 上的一两个按钮,该按钮将显示按下状态并在单击时触发一些操作。所以我想出了另一个解决方案,它分为两部分:

第一部分:
第一部分是能够捕获按钮上的点击以触发某些操作。我的想法如下:

  1. 保留对 InfoWindowAdapter 中创建的自定义 infoWindow 的引用。
  2. 包裹住MapFragment (or MapView)在自定义 ViewGroup 中(我的称为 MapWrapperLayout)
  3. 覆盖MapWrapperLayout的dispatchTouchEvent并且(如果当前显示InfoWindow)首先将MotionEvents路由到先前创建的InfoWindow。如果它不消耗 MotionEvents(例如,因为您没有单击 InfoWindow 内的任何可点击区域等),那么(并且只有这样)才让事件向下传递到 MapWrapperLayout 的超类,以便它最终会传递到地图。

这是MapWrapperLayout的源代码:

package com.circlegate.tt.cg.an.lib.map;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

public class MapWrapperLayout extends RelativeLayout {
    /**
     * Reference to a GoogleMap object 
     */
    private GoogleMap map;

    /**
     * Vertical offset in pixels between the bottom edge of our InfoWindow 
     * and the marker position (by default it's bottom edge too).
     * It's a good idea to use custom markers and also the InfoWindow frame, 
     * because we probably can't rely on the sizes of the default marker and frame. 
     */
    private int bottomOffsetPixels;

    /**
     * A currently selected marker 
     */
    private Marker marker;

    /**
     * Our custom view which is returned from either the InfoWindowAdapter.getInfoContents 
     * or InfoWindowAdapter.getInfoWindow
     */
    private View infoWindow;    

    public MapWrapperLayout(Context context) {
        super(context);
    }

    public MapWrapperLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MapWrapperLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * Must be called before we can route the touch events
     */
    public void init(GoogleMap map, int bottomOffsetPixels) {
        this.map = map;
        this.bottomOffsetPixels = bottomOffsetPixels;
    }

    /**
     * Best to be called from either the InfoWindowAdapter.getInfoContents 
     * or InfoWindowAdapter.getInfoWindow. 
     */
    public void setMarkerWithInfoWindow(Marker marker, View infoWindow) {
        this.marker = marker;
        this.infoWindow = infoWindow;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean ret = false;
        // Make sure that the infoWindow is shown and we have all the needed references
        if (marker != null && marker.isInfoWindowShown() && map != null && infoWindow != null) {
            // Get a marker position on the screen
            Point point = map.getProjection().toScreenLocation(marker.getPosition());

            // Make a copy of the MotionEvent and adjust it's location
            // so it is relative to the infoWindow left top corner
            MotionEvent copyEv = MotionEvent.obtain(ev);
            copyEv.offsetLocation(
                -point.x + (infoWindow.getWidth() / 2), 
                -point.y + infoWindow.getHeight() + bottomOffsetPixels);

            // Dispatch the adjusted MotionEvent to the infoWindow
            ret = infoWindow.dispatchTouchEvent(copyEv);
        }
        // If the infoWindow consumed the touch event, then just return true.
        // Otherwise pass this event to the super class and return it's result
        return ret || super.dispatchTouchEvent(ev);
    }
}

所有这些都将使 InfoView 内的视图再次“活动” - OnClickListeners 将开始触发等。

第二部分:剩下的问题是,显然,您在屏幕上看不到 InfoWindow 的任何 UI 更改。为此,您必须手动调用 Marker.showInfoWindow。现在,如果您在 InfoWindow 中执行一些永久性更改(例如将按钮的标签更改为其他内容),这就足够了。

但显示按钮按下状态或类似性质的状态则更为复杂。第一个问题是,(至少)我无法使信息窗口显示正常按钮的按下状态。即使我按下按钮很长时间,它在屏幕上仍然保持未按下状态。我相信这是由地图框架本身处理的,它可能确保不会在信息窗口中显示任何瞬态状态。但我可能是错的,我并没有试图找出这一点。

我所做的是另一个令人讨厌的黑客 - 我附上了一个OnTouchListener到按钮,并在按下或释放按钮时手动将其背景切换到两个自定义可绘制对象 - 一个按钮处于正常状态,另一个处于按下状态。这不是很好,但它有效:)。现在我可以看到屏幕上的按钮在正常状态和按下状态之间切换。

还有最后一个小故障 - 如果您单击按钮太快,它不会显示按下状态 - 它只是保持正常状态(尽管单击本身被触发,因此按钮“起作用”)。至少在我的 Galaxy Nexus 上是这样的。所以我做的最后一件事就是稍微延迟了按钮的按下状态。这也很丑陋,我不确定它在一些较旧的、缓慢的设备上如何工作,但我怀疑即使是地图框架本身也会做这样的事情。您可以自己尝试一下 - 当您单击整个 InfoWindow 时,它会保持按下状态的时间稍长一些,然后是普通按钮(同样 - 至少在我的手机上)。这实际上就是它在原始谷歌地图应用程序上的工作原理。

不管怎样,我自己编写了一个自定义类来处理按钮状态更改以及我提到的所有其他内容,所以这里是代码:

package com.circlegate.tt.cg.an.lib.map;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.google.android.gms.maps.model.Marker;

public abstract class OnInfoWindowElemTouchListener implements OnTouchListener {
    private final View view;
    private final Drawable bgDrawableNormal;
    private final Drawable bgDrawablePressed;
    private final Handler handler = new Handler();

    private Marker marker;
    private boolean pressed = false;

    public OnInfoWindowElemTouchListener(View view, Drawable bgDrawableNormal, Drawable bgDrawablePressed) {
        this.view = view;
        this.bgDrawableNormal = bgDrawableNormal;
        this.bgDrawablePressed = bgDrawablePressed;
    }

    public void setMarker(Marker marker) {
        this.marker = marker;
    }

    @Override
    public boolean onTouch(View vv, MotionEvent event) {
        if (0 <= event.getX() && event.getX() <= view.getWidth() &&
            0 <= event.getY() && event.getY() <= view.getHeight())
        {
            switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN: startPress(); break;

            // We need to delay releasing of the view a little so it shows the pressed state on the screen
            case MotionEvent.ACTION_UP: handler.postDelayed(confirmClickRunnable, 150); break;

            case MotionEvent.ACTION_CANCEL: endPress(); break;
            default: break;
            }
        }
        else {
            // If the touch goes outside of the view's area
            // (like when moving finger out of the pressed button)
            // just release the press
            endPress();
        }
        return false;
    }

    private void startPress() {
        if (!pressed) {
            pressed = true;
            handler.removeCallbacks(confirmClickRunnable);
            view.setBackground(bgDrawablePressed);
            if (marker != null) 
                marker.showInfoWindow();
        }
    }

    private boolean endPress() {
        if (pressed) {
            this.pressed = false;
            handler.removeCallbacks(confirmClickRunnable);
            view.setBackground(bgDrawableNormal);
            if (marker != null) 
                marker.showInfoWindow();
            return true;
        }
        else
            return false;
    }

    private final Runnable confirmClickRunnable = new Runnable() {
        public void run() {
            if (endPress()) {
                onClickConfirmed(view, marker);
            }
        }
    };

    /**
     * This is called after a successful click 
     */
    protected abstract void onClickConfirmed(View v, Marker marker);
}

这是我使用的自定义 InfoWindow 布局文件:

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginRight="10dp" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="Title" />

        <TextView
            android:id="@+id/snippet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="snippet" />

    </LinearLayout>

    <Button
        android:id="@+id/button" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

测试活动布局文件(MapFragment位于MapWrapperLayout):

<com.circlegate.tt.cg.an.lib.map.MapWrapperLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map_relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</com.circlegate.tt.cg.an.lib.map.MapWrapperLayout>

最后是测试活动的源代码,它将所有这些粘合在一起:

package com.circlegate.testapp;

import com.circlegate.tt.cg.an.lib.map.MapWrapperLayout;
import com.circlegate.tt.cg.an.lib.map.OnInfoWindowElemTouchListener;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {    
    private ViewGroup infoWindow;
    private TextView infoTitle;
    private TextView infoSnippet;
    private Button infoButton;
    private OnInfoWindowElemTouchListener infoButtonListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
        final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout)findViewById(R.id.map_relative_layout);
        final GoogleMap map = mapFragment.getMap();

        // MapWrapperLayout initialization
        // 39 - default marker height
        // 20 - offset between the default InfoWindow bottom edge and it's content bottom edge 
        mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20)); 

        // We want to reuse the info window for all the markers, 
        // so let's create only one class member instance
        this.infoWindow = (ViewGroup)getLayoutInflater().inflate(R.layout.info_window, null);
        this.infoTitle = (TextView)infoWindow.findViewById(R.id.title);
        this.infoSnippet = (TextView)infoWindow.findViewById(R.id.snippet);
        this.infoButton = (Button)infoWindow.findViewById(R.id.button);

        // Setting custom OnTouchListener which deals with the pressed state
        // so it shows up 
        this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
                getResources().getDrawable(R.drawable.btn_default_normal_holo_light),
                getResources().getDrawable(R.drawable.btn_default_pressed_holo_light)) 
        {
            @Override
            protected void onClickConfirmed(View v, Marker marker) {
                // Here we can perform some action triggered after clicking the button
                Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();
            }
        }; 
        this.infoButton.setOnTouchListener(infoButtonListener);


        map.setInfoWindowAdapter(new InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                // Setting up the infoWindow with current's marker info
                infoTitle.setText(marker.getTitle());
                infoSnippet.setText(marker.getSnippet());
                infoButtonListener.setMarker(marker);

                // We must call this to set the current marker and infoWindow references
                // to the MapWrapperLayout
                mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
                return infoWindow;
            }
        });

        // Let's add a couple of markers
        map.addMarker(new MarkerOptions()
            .title("Prague")
            .snippet("Czech Republic")
            .position(new LatLng(50.08, 14.43)));

        map.addMarker(new MarkerOptions()
            .title("Paris")
            .snippet("France")
            .position(new LatLng(48.86,2.33)));

        map.addMarker(new MarkerOptions()
            .title("London")
            .snippet("United Kingdom")
            .position(new LatLng(51.51,-0.1)));
    }

    public static int getPixelsFromDp(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dp * scale + 0.5f);
    }
}

就是这样。到目前为止,我只在我的 Galaxy Nexus (4.2.1) 和 Nexus 7(也是 4.2.1)上进行了测试,如果有机会,我会在一些 Gingerbread 手机上尝试一下。到目前为止,我发现的一个限制是您无法从屏幕上的按钮所在位置拖动地图并四处移动地图。它可能会以某种方式克服,但现在,我可以忍受。

我知道这是一个丑陋的黑客,但我只是没有找到更好的东西,而且我非常需要这种设计模式,这确实是回到地图 v1 框架的一个原因(顺便说一句。我真的很想避免对于带有片段等的新应用程序)。我只是不明白为什么 Google 不向开发人员提供一些在 InfoWindows 上拥有按钮的官方方式。这是一种常见的设计模式,而且这种模式甚至在官方 Google 地图应用程序中也使用:)。我理解为什么他们不能让你的视图在 InfoWindows 中“实时”显示 - 这可能会降低移动和滚动地图时的性能。但应该有一些方法可以在不使用视图的情况下实现这种效果。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google 地图 Android API v2 - 交互式信息窗口(就像原始 Android 谷歌地图一样) 的相关文章

随机推荐