Android 在 MapView 中的地图标记上方显示弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3707923/
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-20 11:28:36  来源:igfitidea点击:

Show popup above map marker in MapView

androidgoogle-maps

提问by fhucho

I can't beleive there's no easy way to do such a basic thing like this... I want to show a popup/baloon (a LinearLayout) after user clicks on a map marker (something smilar to what is in Google Maps app). It should move with the map, when the user scrolls the map. What is the best way to do this?

我不敢相信没有简单的方法可以做这样一个基本的事情......我想在用户点击地图标记(类似于谷歌地图应用程序中的内容)后显示一个弹出窗口/气球(一个线性布局) . 当用户滚动地图时,它应该随地图移动。做这个的最好方式是什么?

One idea is to have the LinearLayout in my Activity's root layout and show it when needed. But how to make it move with the map?

一种想法是在我的 Activity 的根布局中使用 LinearLayout 并在需要时显示它。但是如何让它随地图移动呢?

Another way to do that may be to create an Overlay that draws the LinearLayout in onDraw and gives the layout touch events. Is this possible?

另一种方法可能是创建一个 Overlay,在 onDraw 中绘制 LinearLayout 并提供布局触摸事件。这可能吗?

回答by Kiran

The way I did is:

我的做法是:

Put the markers at required GeoPoints by subclassing ItemizedOverlay, as described in http://developer.android.com/guide/tutorials/views/hello-mapview.html

http://developer.android.com/guide/tutorials/views/hello-mapview.html 中所述,通过子类化 ItemizedOverlay 将标记置于所需的 GeoPoints

Create a popup View by inflating from the layout:

通过从布局中膨胀来创建一个弹出视图:

View popUp = getLayoutInflater().inflate(R.layout.map_popup, map, false);

Use MapView.LayoutParams to position the popup with respect to GeoPoint in the ItemizedOverlay< OverlayItem >::onTap method. Popup will scroll automatically (without any additional code) when user scrolls the map. Basically popup gets tied to a GeoPoint, if user zooms, popup's position gets adjusted automatically.

使用 MapView.LayoutParams 在 ItemizedOverlay< OverlayItem >::onTap 方法中相对于 GeoPoint 定位弹出窗口。当用户滚动地图时,弹出窗口将自动滚动(无需任何额外代码)。基本上弹出窗口与 GeoPoint 相关联,如果用户缩放,弹出窗口的位置会自动调整。

MapView map = (MapView) findViewById(R.id.mapview);   
MapView.LayoutParams mapParams = new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        <geopoint>,
                        <x offset if required>,
                        <y offset like pinHeight>,
                        MapView.LayoutParams.BOTTOM_CENTER);
map.addView(popUp, mapParams);

回答by OneWorld

Here is the "the missing Widget"...

这是“丢失的小部件”...