Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 26, 2025

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 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.screenPosFromCoord()
  • 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 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.

Copilot AI and others added 3 commits August 26, 2025 05:53
Co-authored-by: maerki <6221466+maerki@users.noreply.github.com>
…ation

Co-authored-by: maerki <6221466+maerki@users.noreply.github.com>
Copilot AI changed the title [WIP] Anchors Implement MCMapAnchor for positioning UIKit views relative to map coordinates Aug 26, 2025
Copilot AI requested a review from maerki August 26, 2025 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anchors

2 participants