Android Google Maps v2:如何使用多行代码段添加标记?

unc*_*Lem 64 android google-maps

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

map.getMap().addMarker(new MarkerOptions()
    .position(latLng()).snippet(snippetText)
    .title(header).icon(icon));
Run Code Online (Sandbox Code Playgroud)

我想要片段看起来像这样:

| HEADER |
|foo     |
|bar     |
Run Code Online (Sandbox Code Playgroud)

但是当我试图将snippetText设置为"foo \n bar"时,我看到了foo bar,我没有任何想法如何使它成为多线.你能帮助我吗?

Hir*_*tel 115

我用最简单的方式做了如下:

private GoogleMap mMap;
Run Code Online (Sandbox Code Playgroud)

虽然添加 标记谷歌地图:

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)));
Run Code Online (Sandbox Code Playgroud)

之后在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;
    }
});
Run Code Online (Sandbox Code Playgroud)

希望它会对你有所帮助.

  • 该解决方案实际上正在运行 +1用于添加代码示例. (2认同)
  • 它确实处理标题和许多行片段.谢谢. (2认同)
  • 由于“代码片段”,这应该被接受。节省了我的时间。甚至我喜欢 `mMap` 变量名,因为我也没有改变。 (2认同)

Com*_*are 58

您似乎需要创建自己的"信息窗口"内容才能使其工作:

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

  2. 打电话setInfoWindowAdapter()给你GoogleMap,传递你的实例InfoWindowAdapter

该示例项目演示了该技术."foo\nbar"正确处理换行符替换我的代码片段.但是,更有可能的是,您只需提出一个布局,避免使用换行符,并TextView在您想要的视觉效果中为每一行添加单独的小部件.


Nub*_*per 15

建立在Hiren Patel的回答上,Andrew S建议:

 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;
        }
    });
Run Code Online (Sandbox Code Playgroud)