最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - (iOS 18+ MapKit) Custom UserAnnotation annotation view - Stack Overflow

programmeradmin3浏览0评论

I'm implementing a Map with user location customization in SwiftUI using iOS 17+ MapKit APIs. When using the selection parameter with Map, the default blue dot user location becomes tappable but shows an empty annotation view. However, using UserAnnotation makes the location marker non-interactive.

My code structure:

import SwiftUI
import MapKit

struct UserAnnotationSample: View {
    @State private var position: MapCameraPosition = .userLocation(fallback: .automatic)
    @State private var selectedItem: MapSelection<MKMapItem>?

    var body: some View {
        Map(position: $position, selection: $selectedItem) {
            // UserAnnotation() 
        }
        .mapControls {
            MapUserLocationButton()
        }
    }
}

Key questions:

  1. How can I replace the empty annotation view with a custom avatar when tapping the user location?
  2. Is there a way to make UserAnnotation interactive with selection?
  3. Should I use tag modifier for custom annotations? What's the proper way to associate selections?

screenshot

发布评论

评论列表(0)

  1. 暂无评论