我的目标是在点击按钮时更改标记标题。问题是我无法在onCreate()
中获取标记并设置标题,因为它已在onMapReady()
中初始化。我有以下代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
updateMarkerTexts(marker, "ss", "gg");
}
});
return view;
}
和
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
enableMyLocation();
onMyLocationButtonClick();
final Marker marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(51.2001, 62.9187))
.title("Some title")
.snippet(getResources().getString(R.string.somestr))));
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService (Context.LAYOUT_INFLATER_SERVICE);
//some params
}
@Override
public View getInfoContents(Marker arg0) {
return null;
}
getInfoWindow(marker);
marker.showInfoWindow();
}
答案 0 :(得分:3)
只需将您的标记实例保存在您的类中,而不是将其保存在onMapReady范围内:
Marker marker;
@Override
public void onMapReady(final GoogleMap googleMap) {
...
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(51.2001, 62.9187))
.title("Some title")
.snippet(getResources().getString(R.string.somestr))));