To achieve what you want you'll have to publish markers.
The processe is basically the same as in ROS1. As most of the things in ROS2.
Here's an example: http://library.isr.ist.utl.pt/docs/roswiki/rviz(2f)Tutorials(2f)Markers(3a20)Basic(20)Shapes.html
You can use something like this:
#include <chrono>
#include "rclcpp/rclcpp.hpp"
#include <visualization_msgs/msg/marker.hpp>
class pub_view : public rclcpp::Node
{
visualization_msgs::msg::Marker marker;
rclcpp::Publisher<visualization_msgs::msg::Marker>::SharedPtr marker_pub;
public:
pub_view()
: Node("pub_view"), count_(0)
{
marker_pub = this->create_publisher<visualization_msgs::msg::Marker>("Origin",10);
marker.header.frame_id = "map";
marker.header.stamp = this->get_clock().get()->now();
marker.id = 0;
marker.type = visualization_msgs::msg::Marker::SPHERE;
marker.action = visualization_msgs::msg::Marker::ADD;
marker.pose.position.x = 0; marker.pose.position.y = 0; marker.pose.position.z = 0;
marker.pose.orientation.x = 0; marker.pose.orientation.y = 0; marker.pose.orientation.z = 0; marker.pose.orientation.w = 0;
marker.scale.x = 2.5; marker.scale.y = 2.5; marker.scale.z = 2.5;
marker.color.r = 1.0; marker.color.g = 0.0; marker.color.b = 0.0; marker.color.a = 0.5;
}
pub_sphere(){
// Update timestamp
marker.action = visualization_msgs::msg::Marker::MODIFY;
marker.header.stamp = this->get_clock().get()->now();
// Update pose
marker.pose.position.x = 0; marker.pose.position.y = 0; marker.pose.position.z = 0;
marker.pose.orientation.x = 0; marker.pose.orientation.y = 0; marker.pose.orientation.z = 0; marker.pose.orientation.w = 0;
marker_pub->publish(marker);
}
}
This code publishes a sphere marker. I did not tested the code but it should be something similar to this. Make sure that you're using the correct 'frame_id' or you'll not be able to visualize the markers on rviz.