gpt4 book ai didi

java - 如何仅为特定标记设置 InfoWindowAdapter

转载 作者:行者123 更新时间:2023-11-30 10:31:32 28 4
gpt4 key购买 nike

我正在尝试在我的 map View 中为特定标记使用“setInfoWindowAdapter”属性,但是对于“标记 2”,由于某种原因(标记 2),标题下方似乎出现了一些不需要的空间(见附件截图)。可以做些什么来防止这种情况发生?

FragmentCustomMapview.java

public class FragmentCustomMapview extends android.support.v4.app.Fragment implements OnMapReadyCallback {

public FragmentCustomMapview() {
// Required empty constructor
}

GoogleMap mGoogleMap;
MapView mMapView;
SwitchCompat swt;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_custommapview, container, false);

mMapView = (MapView) v.findViewById(R.id.map_custommapview);
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(this); //this is important

swt = (SwitchCompat) v.findViewById(R.id.switch_map_custommapview);

return v;
}

@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mGoogleMap.setBuildingsEnabled(true);
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
initMap(isChecked);
}
});

initMap(swt.isChecked());

// Add markers and move the camera
LatLng marker1 = new LatLng(51.510256, -0.135106);
mGoogleMap.addMarker(new MarkerOptions()
.position(marker1)
.title("Lorem ipsum dolor")
.snippet("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
);

LatLng marker2 = new LatLng(51.509793, -0.134961);
mGoogleMap.addMarker(new MarkerOptions()
.position(marker2)
.title("Marker 2")
);


mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}

@Override
public View getInfoContents(Marker marker) {

Context mContext = getActivity();

LinearLayout info = new LinearLayout(mContext);
info.setOrientation(LinearLayout.VERTICAL);

TextView title = new TextView(mContext);
title.setTextColor(Color.BLACK);
title.setGravity(Gravity.CENTER);
title.setTypeface(null, Typeface.BOLD);
title.setText(marker.getTitle());
title.setSingleLine(false);

TextView snippet = new TextView(mContext);
snippet.setTextColor(Color.GRAY);
snippet.setText(marker.getSnippet());
snippet.setMultiline(false)

info.addView(title);
info.addView(snippet);

return info;
}
});

// Updates the location and zoom level of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(51.509932, -0.134720), 18);
mGoogleMap.animateCamera(cameraUpdate);
}

@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}

@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}

@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}

@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}

private void initMap(boolean isChecked){
if (isChecked) {
mGoogleMap.setMapStyle(new MapStyleOptions(getResources().getString(R.string.style_json)));
} else {
mGoogleMap.setMapStyle(null);
}
}
}

第一个标记

enter image description here

第二个标记

enter image description here

最佳答案

如果您的 Marker 没有代码段,请不要将您的 TextView 代码段 添加到您的 LinearLayout 信息:

@Override
public View getInfoContents(Marker marker) {

Context mContext = getActivity();

LinearLayout info = new LinearLayout(mContext);
info.setOrientation(LinearLayout.VERTICAL);

TextView title = new TextView(mContext);
title.setTextColor(Color.BLACK);
title.setGravity(Gravity.CENTER);
title.setTypeface(null, Typeface.BOLD);
title.setText(marker.getTitle());
title.setSingleLine(false);

info.addView(title);

if (marker.getSnippet() != null) {
TextView snippet = new TextView(mContext);
snippet.setTextColor(Color.GRAY);
snippet.setText(marker.getSnippet());
snippet.setSingleLine(false);

info.addView(snippet);
}

return info;
}

关于java - 如何仅为特定标记设置 InfoWindowAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43158663/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com