最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Google Maps iOS SDK: Custom marker iconView with SwiftUI - Stack Overflow

matteradmin7PV0评论

I am trying to create a custom iconView using SwiftUI for may Google Maps markers:

struct CustumMarkerView: View {
  let inter15_500 = Font.inter(size: 15, weight: 500)
  let destination: POILocation
  
  var iconName: String {
    switch destination.locationType.id {
    case 1:
      return "airplane"
    case 2:
      return "bus"
    case 3:
      return "train.side.front.car"
    case 4:
      return "building.2"
    default:
      return "party.popper"
    }
  }
  
  var body: some View {
    HStack(alignment: .center, spacing: 4) {
      Text(destination.name)
        .font(inter15_500)
        .foregroundStyle(Theme.Color.black2)
        .lineLimit(1)
      Image(systemName: iconName)
        .resizable()
        .renderingMode(.template)
        .aspectRatio(contentMode: .fit)
        .padding(8)
        .frame(width: 32, height: 32)
        .background(Theme.Color.black6)
        .foregroundStyle(.white)
        .clipShape(.circle)
    }
    .frame(height: 32)
  }
}

@MainActor
class CustomMarker: GMSMarker {
  
  let destination: POILocation
  
  init(destination: POILocation) {
    self.destination = destination
    super.init()
    
    let swiftUIView = UIHostingController(rootView: CustumMarkerView(destination: destination)).view!
    swiftUIView.translatesAutoresizingMaskIntoConstraints = false
    swiftUIView.backgroundColor = .green
    //swiftUIView.heightAnchor.constraint(equalToConstant: 32).isActive = true
    self.iconView = swiftUIView
    self.appearAnimation = .fadeIn
    
    self.position = .init(
      latitude: Double(destination.coordinate.latitude) ?? 0,
      longitude: Double(destination.coordinate.longitude) ?? 0
    )
  }
}

If I don't add a height constraint it becomes like this:

If I add a height constraint it becomes like this:

Can anyone help me fix it?

Post a comment

comment list (0)

  1. No comments so far