70

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

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

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

| HEADER |
|foo     |
|bar     |

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

4

6 回答 6

135

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

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 Map上为InfoWindow 适配器添加以下代码:

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

希望它会帮助你。

于 2015-07-25T17:38:32.287 回答
62

看起来您需要创建自己的“信息窗口”内容才能使其正常工作:

  1. 创建一个InfoWindowAdapter覆盖的实现getInfoContents()以返回您想要进入InfoWindow框架的内容

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

此示例项目演示了该技术。"foo\nbar"用正确处理换行符替换我的片段。但是,更有可能的是,您只会想出一个无需换行符的布局,并TextView为您想要的视觉结果中的每一行提供单独的小部件。

于 2012-12-16T19:34:27.393 回答
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;
        }
    });
于 2015-08-06T09:33:59.443 回答
2

基于Hiren Patel解决方案。此代码创建一个TextView从布局,而不是从零开始。一个显着的区别:如果您有clusters ,则在单击集群时不会看到void 标签。

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

布局标记.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"
    />
于 2020-01-28T16:06:12.213 回答
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
        }
    })
于 2020-07-22T18:58:08.400 回答
-1

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);
于 2018-07-25T09:39:38.540 回答