首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android Google Maps v2:如何添加带有多行代码段的标记?

Android Google Maps v2:如何添加带有多行代码段的标记?
EN

Stack Overflow用户
提问于 2012-12-17 03:16:41
回答 3查看 76.3K关注 0票数 70

谁知道如何将多行代码片段添加到Google Maps标记?这是我添加标记的代码:

代码语言:javascript
运行
复制
map.getMap().addMarker(new MarkerOptions()
    .position(latLng()).snippet(snippetText)
    .title(header).icon(icon));

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

代码语言:javascript
运行
复制
| HEADER |
|foo     |
|bar     |

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

EN

回答 3

Stack Overflow用户

发布于 2015-08-06 17:33:59

按照安德鲁·S的建议在Hiren Patel's answer上构建:

代码语言:javascript
运行
复制
 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;
        }
    });
票数 15
EN

Stack Overflow用户

发布于 2020-07-23 02:58:08

相同的代码,但在Kotlin中:

代码语言:javascript
运行
复制
    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
EN

Stack Overflow用户

发布于 2018-07-25 17:39:39

新建(mMap.setOnMapClickListener GoogleMap.OnMapClickListener() {

代码语言:javascript
运行
复制
        @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);
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13904651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档