Geolocation and Maps in C# (.NET): Overview of Key Libraries and Tools
Working with geodata and cartography is a popular task in modern .NET applications. From location tracking to map visualization, the C# ecosystem offers several powerful solutions. In this article, we will review the main libraries for working with geolocation and maps, their capabilities, and usage examples.
1. Built-in Library System.Device.Location (GeoCoordinate)
For basic work with GPS coordinates in .NET Framework, the GeoCoordinate class from the System.Device.Location namespace is available. It allows you to obtain the current device location (Windows) and calculate the distance between two points.
Usage example:
using System.Device.Location;
var watcher = new GeoCoordinateWatcher();watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));
if (watcher.Status == GeoPositionStatus.Ready){ GeoCoordinate coord = watcher.Position.Location; Console.WriteLine($"Latitude: {coord.Latitude}, Longitude: {coord.Longitude}");}
// Distance between two pointsvar point1 = new GeoCoordinate(55.7558, 37.6176); // Moscowvar point2 = new GeoCoordinate(59.9343, 30.3351); // Saint Petersburgdouble distance = point1.GetDistanceTo(point2); // in metersConsole.WriteLine($"Distance: {distance / 1000:F2} km");2. NetTopologySuite — Advanced Geometry Handling
NetTopologySuite (NTS) is a .NET port of the popular Java library JTS. It provides powerful tools for working with geometric objects (points, lines, polygons) and spatial operations (intersection, union, buffering).
Key features:
- Creation and analysis of geometric shapes (Point, LineString, Polygon).
- Spatial predicates (Contains, Intersects, Within).
- Coordinate transformation and projection (together with ProjNet).
- Reading/writing WKT, WKB, GeoJSON.
Example:
using NetTopologySuite.Geometries;using NetTopologySuite.IO;
var geometryFactory = new GeometryFactory();Point point1 = geometryFactory.CreatePoint(new Coordinate(37.6176, 55.7558));Point point2 = geometryFactory.CreatePoint(new Coordinate(30.3351, 59.9343));
double distance = point1.Distance(point2); // in degreesConsole.WriteLine($"Distance in degrees: {distance:F4}");
// Reading from WKTvar reader = new WKTReader();Polygon polygon = reader.Read("POLYGON ((30 20, 45 40, 10 40, 30 20))") as Polygon;Console.WriteLine($"Polygon area: {polygon.Area}");3. Mapsui — Cross-Platform Mapping Library
Mapsui is a modern library for displaying maps in .NET (Windows, Android, iOS, WebAssembly). It supports various tile sources (OpenStreetMap, Bing, MapBox), as well as rendering custom layers (markers, lines, polygons).
Key features:
- Support for WPF, MAUI, Avalonia, Blazor.
- Simple API for adding markers and layers.
- Animation and interaction (zooming, panning).
- Ability to use offline maps (MBTiles).
Example for WPF:
using Mapsui;using Mapsui.Projections;using Mapsui.Tiling;
var map = new Map();map.Layers.Add(OpenStreetMap.CreateTileLayer());
// Adding a markervar pin = new PointFeature(37.6176, 55.7558);pin["label"] = "Moscow";map.Layers.Add(new Layer { Name = "Markers", DataSource = new MemoryProvider(pin) });
// In XAML: <MapControl Map="{Binding Map}" />// Example binding in code:// mapControl.Map = map;4. SharpMap — Classic Library for Geographic Information Systems
SharpMap —
Also in library
Working with ZIP Archives in Go (Golang) — Complete Guide
Reactive Programming in Swift: Combine and RxSwift for iOS/macOS
JWT Tokens in Go (Golang): golang-jwt Library and Usage Examples
Rate Limiting in TypeScript: Libraries, Strategies, and Examples