maps-core
maps-core copied to clipboard
Implement MCMapAnchor for positioning UIKit views relative to map coordinates
This PR introduces a new "anchors" concept to the iOS MCMapView that enables positioning UIKit views relative to map coordinates using standard AutoLayout constraints. Views anchored to map coordinates automatically update their position when the map camera changes (pan, zoom, rotate).
Key Features
MCMapAnchor Class
-
Modifiable Coordinates: Initialize with
MCCoordand update the coordinate at any time -
AutoLayout Integration: Provides standard
NSLayoutAnchorproperties (centerXAnchor,centerYAnchor,leadingAnchor,trailingAnchor,topAnchor,bottomAnchor) -
Automatic Updates: Listens to camera changes via
MCMapCameraListenerInterfaceand updates screen positions automatically - Efficient Constraint Management: Properly manages internal positioning constraints for optimal performance
MCMapView Extensions
-
createAnchor(for coordinate: MCCoord)- Create new anchors -
removeAnchor(_:)andremoveAllAnchors()- Anchor lifecycle management -
activeAnchors- Access all current anchors - Camera listener integration to update all anchors on map interactions
Usage Example
// Create an anchor for a specific coordinate
let zurichCoord = MCCoord(lat: 47.3769, lon: 8.5417)
let anchor = mapView.createAnchor(for: zurichCoord)
// Position any UIView relative to the map coordinate using AutoLayout
let pinView = UIView()
NSLayoutConstraint.activate([
pinView.centerXAnchor.constraint(equalTo: anchor.centerXAnchor),
pinView.centerYAnchor.constraint(equalTo: anchor.centerYAnchor)
])
// The pin automatically stays positioned at the coordinate as users interact with the map
Implementation Details
- Uses a hidden internal UIView that participates in the layout system
- Converts coordinates to screen positions using
camera.screenPosFromCoord() - Efficiently batches updates only when camera actually changes
- Memory management uses
CameraListenerWrapperwith weak references to prevent retain cycles - Proper constraint cleanup on anchor removal and MapView deallocation
- Thread-safe coordinate updates with main queue layout calls
This enables native iOS AutoLayout workflows for map-based applications, making it easy to create overlays, annotations, and UI elements that stay positioned relative to geographic coordinates.
Fixes #855.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.