标记信息窗口中的 Android 谷歌地图 HTML

标签 android html google-maps infowindow

我目前正在解析一个包含坐标和 html 代码的 JSON 对象,并将它们显示在适用于 Android 的 Google map 上......到目前为止,我有这个方法:

private void createMarkersFromJson(String json) throws JSONException {
// De-serialize the JSON string into an array of objects
    JSONArray jsonArray = new JSONArray(json);
    for (int i = 0; i < jsonArray.length(); i++) {
// Create a marker for each object in the JSON data.
        JSONObject jsonObj = jsonArray.getJSONObject(i);
        mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(
                                jsonObj.getDouble("lat"),
                                jsonObj.getDouble("long")
                        ))
        );
    }
}

以上代码引用:https://gist.github.com/TimPim/5902100

此方法成功显示标记。 假设在我的 JSON 对象的一部分中,我有一个名为“contentString”的记录,其中包含 html 代码。我想知道,我如何能够在信息窗口中正确显示它,就像谷歌在此处为其 Javascript API 显示的那样:

https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple

我看过这个答案并尝试自己实现但没有成功: Android Google Maps V2 Title or Snippets Strings From HTML

如果有人能向我展示更详细的解决方案以及如何将其应用于该领域,将不胜感激。

最佳答案

想通了。将 setUpMapIfNeeded 方法编辑为如下所示:

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {
                    View v = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
                    TextView textView = (TextView) v.findViewById(R.id.marker_label);
                    Spanned spannedContent = Html.fromHtml(marker.getTitle());
                    textView.setText(spannedContent, TextView.BufferType.SPANNABLE);
                    return v;
                }
            });
            setUpMap();
        }
    }
}

其中 marker.getTitle(),在另一个方法(包含我们的 html 内容)中从标记获取标题,并在我们新创建的 InfoWindow 中设置文本。本教程非常有帮助:https://www.youtube.com/watch?v=g7rvqxn8SLg

我还在 res/layout 下创​​建了一个 infowindow_layout.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/station_info_layout"
    android:layout_width="205dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/marker_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/marker_label"
        android:layout_marginLeft="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


</LinearLayout>

如果有人想让我澄清我的答案,请告诉我,我会编辑这篇文章。

关于标记信息窗口中的 Android 谷歌地图 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27825387/

相关文章:

html - css 选择器中的疑问 > [#id (.class1 or .class2)]

html - 向 div 添加额外的填充导致其他 div 向下移动 - CSS?

javascript - 为什么嵌入式谷歌地图会改变 Chrome 的字体渲染?

android - Android TV 上的 Facebook 登录

android - 将它与其他 fragment 一起使用时 ViewPager 中的错误

android - Cordova Android 项目无法编译

android - 画廊android中的图像之间有很大的空间

html - 将空值插入文本输入

android - 从 child Activity 回来后 GoogleMap.moveCamera 不工作

javascript - 谷歌地图 API JavaScript : nearbysearch callback function not calling