谷歌地图标记showinfowWindow自定义Android

时间:2020-06-12 18:05:30

标签: java android google-maps google-maps-markers google-maps-android-api-2

在我的应用中,我必须在位置上显示有关标记的信息。 这是我的工作代码。

mPerth = mMap.addMarker(new MarkerOptions()
        .position(PERTH)
        .title("Perth")
        .snippet("Population: Perth")
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
mPerth.showInfoWindow();

如下图所示: enter image description here

我想看起来像这样: enter image description here 如果您有任何方法,请投票给我。

2 个答案:

答案 0 :(得分:0)

尝试一下。

marker.setInfoWindowAnchor(1f,1f)

https://developers.google.com/android/reference/com/google/android/gms/maps/model/Marker#public-void-setinfowindowanchor-float-anchoru,-float-anchorv

编辑:

对于自定义infoWindow,您必须具体实现 GoogleMap.InfoWindowAdapter ,并在getInfoWindow(Marker marker)方法中添加 custom_info_window_layout.xml 文件。

>
public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

Activity context;
CustomInfoWindow(Activity  context){
    this.context =context;
}

@Override
public View getInfoWindow(Marker marker) {
    View view = context.getLayoutInflater().inflate(R.layout.custom_info_window_layout, null);

    TextView title =(TextView) view.findViewById(R.id.tv_title);
    TextView snippet =(TextView) view.findViewById(R.id.tv_snippet);

    title.setText(marker.getTitle());
    snippet.setText(marker.getSnippet());

    return view;
}

@Override
public View getInfoContents(Marker marker) {
    return null;
}

}

最后在地图上设置此 CustomInfoWindowAdapter

map.setInfoWindowAdapter(new CustomInfoWindowAdapter(MainActivity.this))

答案 1 :(得分:0)

static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker marker = mMap.addMarker(new MarkerOptions()
                    .position(PERTH)
                    .anchor(0.5,0.5)
                    .rotation(90.0)
                    .infoWindowAnchor(0.5,0)); // add this line

enter image description here

指定标记图像中显示信息窗口时锚定的点。默认值为图片的顶部中间。