谷歌地图:你可以在片段里面制作字符串吗?

时间:2017-04-12 15:49:34

标签: java android google-maps infowindow

我有一段时间试图创建一个显示传感器数据的自定义信息窗口。之前我的问题是我从未解决过如何为不同的标记制作不同的信息窗口。所有标记信息窗口都使用新数据进行了更新。

因此,为了解决这个问题,我正在考虑使用原始信息窗口。

我的代码看起来像

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("co: " +String.valueOf(co_mV) + " mV");
    markerOptions.snippet("no2: " + String.valueOf(co_mV) + " mV");

如果不创建自己的信息窗口,是否可以使代码段粗体或标题不粗体?因为这可以解决我的问题。

感谢。

编辑尝试使用自定义信息windwow。这样就可以在获得新数据时更新所有标记信息窗口。

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();

    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

        public View getInfoWindow(Marker arg0) {
            View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
            TextView tv = (TextView) v.findViewById(R.id.infoText);
            tv.setText("CO2 data: "+String.valueOf(co_mV) + "mV" +"\n" + "N2 data: "+String.valueOf(no2_mV) +" mV");

            return v;
        }

        public View getInfoContents(Marker arg0) {


            return null;

        }
    });


}

1 个答案:

答案 0 :(得分:0)

您可以创建自己的自定义信息窗口并设置内容样式。

<强> custom_info.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#fff"
android:padding="10dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:textColor="@color/black"
    android:id="@+id/title"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <TextView
    android:textColor="@color/black"
    android:id="@+id/snippet"
    android:textStyle="bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

然后创建自定义信息窗口,如下所示

googlemap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
        View v = getLayoutInflater().inflate(R.layout.custom_info, null);
        TextView title= (TextView) v.findViewById(R.id.title);
        TextView snippet= (TextView) v.findViewById(R.id.snippet);
        title.setText("your value");
        snippet.setText("your value");
        return v;
        }
@Override
public View getInfoContents(Marker arg0) {
        return  null;

        }
        });

markerOptions.showInfoWindow();