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-2020

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

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


posted @ 2022-06-04 11:32  xgqfrms  阅读(83)  评论(1编辑  收藏  举报