As AIS data play an increasingly important role in intelligent shipping and shipping regulation, research on AIS trajectories has attracted more attention. Effective measurement is a critical issue in AIS trajectory research. It directly impacts downstream research areas such as anomaly detection, trajectory clustering, and trajectory prediction. However, the extremely time-consuming and labor-intensive traditional pairwise methods for calculating different types of distances between trajectories hinders the large-scale application and further analysis of AIS data. To tackle these issues, we introduce AISim—a metric learning framework that utilizes heterogeneous graph neural networks. This framework includes a spatial pre-training graph and a hierarchical heterogeneous graph, which incorporate spatial and sequential dependency to extract latent features from vessel trajectories. This approach enhances the model’s ability to capture a more accurate representation of the trajectories and approximate various similarity measurements. Extensive experiments on multiple real trajectory datasets have verified the effectiveness and generality of the proposed framework. AISim outperforms advanced learning-based models by 5% to 66% on the HR10 metric in top-k search tasks. The experimental results demonstrate that the proposed framework facilitates research on AIS trajectory similarity learning, thereby promoting the development of AIS trajectory analysis.