在Android中实现为两个标记显示不同的自定义信息窗口,可以按照以下步骤进行:
以下是一个示例代码,演示如何实现为两个标记显示不同的自定义信息窗口:
// 创建自定义信息窗口适配器类
class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private final View mWindow;
private final View mContents;
CustomInfoWindowAdapter(Context context) {
mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window, null);
mContents = LayoutInflater.from(context).inflate(R.layout.custom_info_contents, null);
}
@Override
public View getInfoWindow(Marker marker) {
// 设置自定义信息窗口的内容
TextView title = mWindow.findViewById(R.id.title);
title.setText(marker.getTitle());
TextView snippet = mWindow.findViewById(R.id.snippet);
snippet.setText(marker.getSnippet());
return mWindow;
}
@Override
public View getInfoContents(Marker marker) {
// 返回null,以便调用getInfoWindow()方法
return null;
}
}
// 在地图的OnMapReady回调方法中设置自定义信息窗口适配器
@Override
public void onMapReady(GoogleMap googleMap) {
// 获取GoogleMap对象
mMap = googleMap;
// 创建自定义信息窗口适配器
CustomInfoWindowAdapter infoWindowAdapter = new CustomInfoWindowAdapter(this);
// 设置自定义信息窗口适配器
mMap.setInfoWindowAdapter(infoWindowAdapter);
// 添加标记
Marker marker1 = mMap.addMarker(new MarkerOptions()
.position(new LatLng(37.7749, -122.4194))
.title("Marker 1")
.snippet("Custom Info Window 1"));
Marker marker2 = mMap.addMarker(new MarkerOptions()
.position(new LatLng(37.7749, -122.4294))
.title("Marker 2")
.snippet("Custom Info Window 2"));
}
在上述示例代码中,我们创建了一个CustomInfoWindowAdapter类来实现自定义信息窗口的样式和内容。在onMapReady()方法中,我们创建了一个CustomInfoWindowAdapter对象,并将其设置为GoogleMap的信息窗口适配器。然后,通过调用addMarker()方法添加两个标记,并为每个标记设置不同的标题和内容。
请注意,上述示例代码中没有提及任何特定的云计算品牌商,如腾讯云。如果需要使用腾讯云相关产品来支持地图功能,可以参考腾讯云地图服务相关文档和API来集成。
领取专属 10元无门槛券
手把手带您无忧上云