Android Google Maps v2:如何添加具有多行片段的标记?

75

有人知道如何在Google地图标记上添加多行片段吗?这是我添加标记的代码:

map.getMap().addMarker(new MarkerOptions()
    .position(latLng()).snippet(snippetText)
    .title(header).icon(icon));

我希望代码片段看起来像这样:

| HEADER |
|foo     |
|bar     |

但是当我试图将snippetText设置为“foo \n bar”时,我只看到foo bar,我不知道如何使它多行。你能帮我吗?

6个回答

141

我用最简单的方法完成了如下:

private GoogleMap mMap;

在向谷歌地图添加标记时:

LatLng mLatLng = new LatLng(YourLatitude, YourLongitude);

mMap.addMarker(new MarkerOptions().position(mLatLng).title("My Title").snippet("My Snippet"+"\n"+"1st Line Text"+"\n"+"2nd Line Text"+"\n"+"3rd Line Text").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

然后将以下代码放入 Google 地图InfoWindow adapter 中:

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

      @Override
      public View getInfoWindow(Marker arg0) {
         return null;
      }

      @Override
      public View getInfoContents(Marker marker) {

        LinearLayout info = new LinearLayout(mContext);
        info.setOrientation(LinearLayout.VERTICAL);

        TextView title = new TextView(mContext);
        title.setTextColor(Color.BLACK);
        title.setGravity(Gravity.CENTER);
        title.setTypeface(null, Typeface.BOLD);
        title.setText(marker.getTitle());

        TextView snippet = new TextView(mContext);
        snippet.setTextColor(Color.GRAY);
        snippet.setText(marker.getSnippet());

        info.addView(title);
        info.addView(snippet);

      return info;
    }
});

希望这能对您有所帮助。


2
这个解决方案实际上是有效的。+1 为添加代码示例。 - MetaSnarf
1
谢谢!snippet加一分。 - LinusGeffarth
1
工作得很好,标题和多个片段行!+1 - Ely Dantas
2
这应该是被接受的答案,因为有“代码片段”。节省了我的时间。我甚至喜欢mMap变量名,因为我也没有改过它。 - Pratik Butani
警告:我将“MapsActivity.this”替换为mContext以避免错误运行。 - Bay

62
看起来你需要创建自己的“信息窗口”内容才能使其工作:
  1. 创建一个实现 InfoWindowAdapter 接口的类,并重写 getInfoContents() 方法以返回你想要放入 InfoWindow 框架中的内容

  2. 在你的 GoogleMap 上调用 setInfoWindowAdapter() 方法,传递你的 InfoWindowAdapter 实例

这个示例项目 演示了这种技术。将我的片段替换为 "foo\nbar" 将正确处理换行符。但更有可能的是,你会设计一个避免需要换行符的布局,并为你所期望的可视化结果的每一行使用单独的 TextView 组件。


如果您使用getInfoWindow()返回完全自定义的视图,请注意,当使用RelativeLayout时似乎存在一个错误。只有在切换到LinearLayout时,我才能使我的工作正常。 我已经提交了一个错误报告: http://code.google.com/p/gmaps-api-issues/issues/detail?id=4874 - Till - Appviewer.io
@Till:通常情况下,当您使用一个RelativeLayout作为根布局资源时,在充气时,您需要使用三个参数的inflate()方法,将父ViewGroup和最后两个参数设置为false。然而,在getInfoContents()中我们没有传递父级,所以我们无法这样做。虽然我对NullPointerException有点惊讶,但我一点也不惊讶RelativeLayout不能正常工作。我本来期望RelativeLayout的子元素的某些布局规则被忽略或者出现其他问题。 - CommonsWare
@CommonsWare 我正在阅读你的4.6书中的Maps V2 Chapter,我有一个与这个问题相关的问题,所以我认为这是一个好地方来问。我想知道是否有办法为每个标记设置不同的infoWindow,只要将setInfoWindowAdapter()应用于整个地图? - AlexAndro
我已经让示例项目的代码运行起来了。所以我有一个自定义的信息窗口,但是我卡在如何添加多行上了。我在XML布局中的标题和片段后面添加了额外的TextView,但是如何让文本出现在新的TextView中呢? - Paul Alexander
1
@cbrook:在TextView上调用setText(),与在活动、片段、ListView行等中的TextView没有区别。 - CommonsWare
这正是我想的,但出于某些原因它对我不起作用 :/ 我所做的就是在片段后在xml中放置第二个textview,然后在getInfoContents中识别它并设置其文本。嗯,也许我应该发一个新问题? - Paul Alexander

15

在 Andrew S 建议下,基于 Hiren Patel 的答案 进行改进:

 mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {

            Context context = getApplicationContext(); //or getActivity(), YourActivity.this, etc.

            LinearLayout info = new LinearLayout(context);
            info.setOrientation(LinearLayout.VERTICAL);

            TextView title = new TextView(context);
            title.setTextColor(Color.BLACK);
            title.setGravity(Gravity.CENTER);
            title.setTypeface(null, Typeface.BOLD);
            title.setText(marker.getTitle());

            TextView snippet = new TextView(context);
            snippet.setTextColor(Color.GRAY);
            snippet.setText(marker.getSnippet());

            info.addView(title);
            info.addView(snippet);

            return info;
        }
    });

2

根据Hiren Patel的解决方案,此代码从布局中创建TextView,而不是从零开始。一个重要的区别是:如果您有群集,则在单击群集时不会看到空标签

override fun onMapReady(googleMap: GoogleMap) {
    this.googleMap = googleMap
    ...

    // Use this anonymous class or implement GoogleMap.InfoWindowAdapter.
    googleMap.setInfoWindowAdapter(object : GoogleMap.InfoWindowAdapter {
        override fun getInfoContents(marker: Marker): View? {
            return null
        }

        override fun getInfoWindow(marker: Marker): View? =
            if (marker.title == null)
                null
            else {
                val inflater = LayoutInflater.from(context)
                val view = inflater.inflate(R.layout.layout_marker, null, false)
                view.label.text = marker.title
    
                view
            }
    })

layout_marker.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:textColor="$f0f0f0"
    android:textSize="12sp"
    tools:text="Marker"
    android:background="#00aaee"
    />

1
相同的代码,但是使用 Kotlin 编写:

    mMap?.setInfoWindowAdapter(object : InfoWindowAdapter {
        override fun getInfoWindow(arg0: Marker): View? {
            return null
        }
        
        override fun getInfoContents(marker: Marker): View {
            val info = LinearLayout(applicationContext)
            info.orientation = LinearLayout.VERTICAL
            val title = TextView(applicationContext)
            title.setTextColor(Color.BLACK)
            title.gravity = Gravity.CENTER
            title.setTypeface(null, Typeface.BOLD)
            title.text = marker.title
            val snippet = TextView(applicationContext)
            snippet.setTextColor(Color.GRAY)
            snippet.text = marker.snippet
            info.addView(title)
            info.addView(snippet)
            return info
        }
    })

-1

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {

            // Already two locations
            if (markerPoints.size() > 1) {
                markerPoints.clear();
                mMap.clear();
            }

            // Adding new item to the ArrayList
            markerPoints.add(point);

            // Creating MarkerOptions
            MarkerOptions options = new MarkerOptions();

            // Setting the position of the marker

            options.position(point);
            if (markerPoints.size() == 1) {
                options.icon(BitmapDescriptorFactory.fromResource(R.mipmap.markerss)).title("Qtrip").snippet("Balance:\nEta:\nName:");
                options.getInfoWindowAnchorV();
            } else if (markerPoints.size() == 2) {
                options.icon(BitmapDescriptorFactory.fromResource(R.mipmap.markerss)).title("Qtrip").snippet("End");
            }
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

                @Override
                public View getInfoWindow(Marker arg0) {
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {

                    Context context = getApplicationContext(); //or getActivity(), YourActivity.this, etc.

                    LinearLayout info = new LinearLayout(context);
                    info.setOrientation(LinearLayout.VERTICAL);

                    TextView title = new TextView(context);
                    title.setTextColor(Color.BLACK);
                    title.setGravity(Gravity.CENTER);
                    title.setTypeface(null, Typeface.BOLD);
                    title.setText(marker.getTitle());

                    TextView snippet = new TextView(context);
                    snippet.setTextColor(Color.GRAY);
                    snippet.setText(marker.getSnippet());

                    info.addView(title);
                    info.addView(snippet);

                    return info;
                }
            });
            mMap.addMarker(options);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接