无法设置Google地图的自定义信息窗口

时间:2017-01-12 19:29:29

标签: android google-maps google-maps-api-3 google-maps-markers infowindow

我正在尝试使用此tutorial设置Custom info windows

这是我的代码:

public class MyActivity extends AppCompatActivity implements OnMapReadyCallback, ClickListenerChatFirebase, GoogleMap.InfoWindowAdapter {

public void showMap() {
        venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng))).title(venue.trim()));
        venueMarker.showInfoWindow();
        getInfoWindow(venueMarker);
    }

public AcceptedRequest(LayoutInflater inflater){
        this.inflater = inflater;
    }

    @Override
    public View getInfoWindow(Marker marker) {

        // Getting view from the layout file
//        inflater = getLayoutInflater();
        View v = inflater.inflate(R.layout.venue_infowindow_background, null);

        TextView title = (TextView) v.findViewById(R.id.venue_txt);
        title.setText(marker.getTitle());

        return v;
    }

    @Override
    public View getInfoContents(Marker arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

}

这是venue_infowindow_background.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:background="@color/colorPrimary"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <TextView
        android:id="@+id/venue_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="venue"
        android:textSize="14sp"
        android:textStyle="bold"
        android:textColor="@android:color/white"/>

</LinearLayout>

问题是没有发生任何变化,信息窗口显示默认的文字颜色和背景颜色。

请帮助解决这个问题。

1 个答案:

答案 0 :(得分:1)

您不能直接调用getInfoWindow()方法覆盖。

您需要将Google地图的InfoWindowAdapter设置为您的Activity,后者将实现GoogleMap.InfoWindowAdapter界面。

public void showMap() {
    venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng))).title(venue.trim()));
    venueMarker.showInfoWindow();

    //This won't work:
    //getInfoWindow(venueMarker); 

    //Do this instead:
    mMap.setInfoWindowAdapter(this);
}

为了对此标记使用不同的布局,您可以为所有其他标记充气不同的自定义布局:

@Override
public View getInfoWindow(Marker marker) {
    View v = null;
    if (marker.equals(venueMarker)) {
        v = inflater.inflate(R.layout.venue_infowindow_background, null);
    } else {
        v = inflater.inflate(R.layout.some_other_layout, null);
    }

    TextView title = (TextView) v.findViewById(R.id.venue_txt);
    title.setText(marker.getTitle());

    return v;
}

为了将“讲话气泡”与默认标记一起使用,而不是使用一个自定义标记,您可以将getInfoWindow()与自定义标记一起使用,将getInfoContents()用于默认标记。 (更多信息here)。这样的事情可能有用。:

@Override
public View getInfoWindow(Marker marker) {
    if (!marker.equals(venueMarker)) {
        return null;
    }
    View v = inflater.inflate(R.layout.venue_infowindow_background, null);
    TextView title = (TextView) v.findViewById(R.id.venue_txt);
    title.setText(marker.getTitle());

    return v;
}

@Override
public View getInfoContents(Marker arg0) {
    if (marker.equals(venueMarker)) {
        return null;
    }
    View v = inflater.inflate(R.layout.some_other_layout, null);
    TextView title = (TextView) v.findViewById(R.id.venue_txt);
    title.setText(marker.getTitle());

    return v;
}