This document describes the R package gdistance, which calculates various distance measures and routes in geographic grids represented as rasters. It provides functions to calculate least-cost distances accounting for landscape heterogeneity, resistance distances modeling random walks, and random shortest paths. The package represents landscapes as transition matrices storing conductance values between raster cells. It calculates distances between points on these landscapes in a flexible way while correcting for projection and distance distortions.