【问题标题】:Android Google Maps v2: How to add marker with multiline snippet?Android Google Maps v2:如何使用多行代码段添加标记?
【发布时间】:2012-12-16 19:16:40
【问题描述】:

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

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

我希望 sn-p 看起来像这样:

| HEADER |
|foo     |
|bar     |

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

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

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

    private GoogleMap mMap;
    

    Google 地图添加 标记

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

    希望对你有所帮助。

    【讨论】:

    • 这个解决方案确实有效。 +1 用于添加代码示例。
    • 谢谢! +1 snippet
    • 工作得很好,标题和多个 sn-p 行! +1
    • 这应该被接受,因为Code Snippet。节省了我的时间。即使我喜欢mMap 变量名,因为我也没有改变。
    • 警告:我将“MapsActivity.this”替换为 mContext 以正常运行。
    【解决方案2】:

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

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

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

    This sample project 演示了该技术。用"foo\nbar" 替换我的sn-ps 可以正确处理换行符。但是,更有可能的是,您只会想出一个不需要换行符的布局,并为您想要的视觉结果中的每一行提供单独的 TextView 小部件。

    【讨论】:

    • 如果您确实使用 getInfoWindow() 来返回完全自定义的视图,请注意在使用 RelativeLayout 时似乎存在错误。我只能在切换到 LinearLayout 时让我的工作。我提交了一个错误报告:code.google.com/p/gmaps-api-issues/issues/detail?id=4874
    • @Till:通常情况下,当你使用一个以RelativeLayout为根的布局资源时,当你膨胀它时,你想使用三参数inflate()方法,传入父级ViewGroupfalse 用于最后两个参数。但是,我们没有在getInfoContents() 中传递父级,所以我们不能这样做。虽然我对NullPointerException 有点惊讶,但我一点也不惊讶RelativeLayout 不能正常工作。我本来希望RelativeLayout 的孩子的一些布局规则会被忽略或行为不端。
    • @CommonsWare 我正在阅读你的 4.6 书中的Maps V2 Chapter,我有一个与这个问题有关的问题,所以我认为这是一个很好的提问地方。我徘徊是否有任何方法可以为每个标记设置一个不同的infoWindow,只要将setInfoWindowAdapter() 应用于孔图?
    • 我已经让示例项目中的代码工作了。所以我有一个自定义信息窗口,但我仍然坚持如何添加多行。我在 xml 布局中的 title 和 sn-p 之后添加了额外的文本视图,但是如何让文本出现在新的文本视图中?
    • @cbrook:在TextViews 上调用setText(),与活动、片段、ListView 行等中的TextViews 没有区别。
    【解决方案3】:

    按照 Andrew S 的建议,以 Hiren Patel's answer 为基础:

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

    【讨论】:

      【解决方案4】:

      基于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"
          />
      

      【讨论】:

        【解决方案5】:

        相同的代码,但在 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
                }
            })
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-08-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-12-02
            • 2013-03-14
            • 2015-06-04
            • 1970-01-01
            相关资源
            最近更新 更多