xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

SwiftUI custom MapAnnotation All In One

SwiftUI custom MapAnnotation All In One

error

Initializer 'init(coordinateRegion:interactionModes:showsUserLocation:userTrackingMode:annotationItems:annotationContent:)' requires that 'MapAnnotationView' conform to 'MapAnnotationProtocol'

  var body: some View {
    // $var 引用类型, var 值类型
    // Map(coordinateRegion: $region)
    Map(coordinateRegion: $region, annotationItems: locations) { location in
      // MapPin(coordinate: location.location, tint: .accentColor)
      // MapMarker(coordinate: location.location, tint: .accentColor)
      // 自定义标注
      /*
      MapAnnotation(coordinate: location.location) {
        Image("Logo")
          .resizable()
          .scaledToFit()
          .frame(width: 20, height: 20, alignment: .center)
      }
      */
      MapAnnotationView(location);
    }
  }

solution

// MapAnnotationProtocol

  var body: some View {
    // $var 引用类型, var 值类型
    // Map(coordinateRegion: $region)
    Map(coordinateRegion: $region, annotationItems: locations) { location in
      // MapAnnotation ✅
      MapAnnotation(coordinate: location.location) {
        // 自定义标注 ✅
        MapAnnotationView(location)
      }
    }
  }

refs

https://swiftwithmajid.com/2020/07/29/using-mapkit-with-swiftui/

https://www.appcoda.com/swiftui-map/



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(86)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-06-04 js undefined bug All In One
2021-06-04 HTTP 410 浏览器拦截请求 bug
2021-06-04 sendBeacon & Blob & CORS-safelisted values All In One
2021-06-04 js try...catch All In One
2021-06-04 已发送邮件撤回功能 All In One
2020-06-04 foreign language learning
2020-06-04 OLAP
点击右上角即可分享
微信分享提示