Skip to content

Anchors #855

@maerki

Description

@maerki

Implement this:

This PR introduces a new "anchors" concept to the iOS MCMapView that enables positioning UIKit views relative to map coordinates using standard AutoLayout constraints.

Overview
The MCMapAnchor class allows developers to create layout anchors tied to specific map coordinates. These anchors automatically update their screen positions when the map camera changes (pan, zoom, rotate), maintaining AutoLayout relationships between UIKit views and map locations.

Key Features
MCMapAnchor Class
Modifiable Coordinates: Initialize with MCCoord and update the coordinate at any time
AutoLayout Integration: Provides standard NSLayoutAnchor properties (centerXAnchor, centerYAnchor, leadingAnchor, trailingAnchor, topAnchor, bottomAnchor)
Automatic Updates: Listens to camera changes via MCMapCameraListenerInterface and 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(_:) and removeAllAnchors() - 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.coordToViewPosition()
Efficiently batches updates only when camera actually changes
Memory Management: Uses CameraListenerWrapper with weak references to prevent retain cycles
Proper constraint cleanup on anchor removal
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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions