Combing SwiftUI and UIKit to add clustering to existing SwiftUI app using Mapkit

We are building a travel app and have a page with locations (about 1000) mapped using SwiftUI mostly with the latest iOS17 Mapkit code. However, our knowledge is only based in SwiftUI which currently has no native clustering abilities so we need to use UIKit to be able to add the standard iOS clustering. We have a very rough working version in UIKit but it involved changing a ton of the layout code over (which we wanted to avoid) but that may be due to us not knowing enough about how to integrate the two platforms.

Would the attached code be able to be integrated into UIKit to get the clustering working but without having to change all the SwiftUI layout, image, context menu, overlays code – eg keeping as much SwiftUI code as possible and just adding in to the UIKit code where necessary to get the map clustering working?

Appreciate any help you can offer – thanks.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Map(position: $position) {
UserAnnotation()
ForEach(filteredPlaces()) { place in
Annotation("(place.name)n(getSubcategoryName(for: place))", coordinate: CLLocationCoordinate2D(latitude: place.latitude, longitude: place.longitude), anchor: .bottom) {
VStack(spacing: 0) {
Spacer()
if let subCategoryId = place.subCategoryIds.first,
let subCategory = viewModel.subCategories.first(where: { $0.id == subCategoryId }),
let topCategory = viewModel.categories.first(where: { $0.subCategoryIds.contains(subCategoryId) }) {
Image(subCategory.imageUrl + "_icon")
.frame(width: 30, height: 30)
.aspectRatio(contentMode: .fit)
.padding(10)
.foregroundStyle(.white)
.background(
Circle()
.fill(getColorForTopCategory(topCategory.id))
.stroke(Color.white, lineWidth: 2)
)
.overlay(alignment: .topTrailing) {
place.isFavorite ? Image(systemName: "heart.circle.fill")
.symbolRenderingMode(.palette)
.foregroundStyle(place.isFavorite ? .red : .black, place.isFavorite ? .white : .white)
.font(.system(size: 24))
.shadow(radius: 5)
.offset(x: 10, y: -10) : nil
}
} else {
Image(systemName: "mappin.circle.fill")
.frame(width: 30, height: 30)
.aspectRatio(contentMode: .fit)
.padding(10)
.foregroundStyle(.white)
.background(Color.red)
.cornerRadius(30)
}
Image(systemName: "arrowtriangle.down.fill")
.foregroundStyle(getColorForTopCategory(viewModel.categories.first(where: { $0.subCategoryIds.contains(place.subCategoryIds.first ?? 0) })?.id ?? 0))
.frame(height: 5)
}
.onTapGesture {
selectedPlace = place
isSheetPresented = true
getlookAroundScene(for: place)
}
.contextMenu {
ForEach(place.subCategoryIds.compactMap { subCategoryId in
viewModel.subCategories.first(where: { $0.id == subCategoryId })
}, id: .id) { subCategory in
Button("Show Only: (subCategory.name)") {
selectedFilterCategory = subCategory.name
}
}
ForEach(topCategories(for: place), id: .id) { category in
Button("Show Only: (category.name)") {
selectedFilterCategory = category.name
}
}
}
}
}
}
</code>
<code>Map(position: $position) { UserAnnotation() ForEach(filteredPlaces()) { place in Annotation("(place.name)n(getSubcategoryName(for: place))", coordinate: CLLocationCoordinate2D(latitude: place.latitude, longitude: place.longitude), anchor: .bottom) { VStack(spacing: 0) { Spacer() if let subCategoryId = place.subCategoryIds.first, let subCategory = viewModel.subCategories.first(where: { $0.id == subCategoryId }), let topCategory = viewModel.categories.first(where: { $0.subCategoryIds.contains(subCategoryId) }) { Image(subCategory.imageUrl + "_icon") .frame(width: 30, height: 30) .aspectRatio(contentMode: .fit) .padding(10) .foregroundStyle(.white) .background( Circle() .fill(getColorForTopCategory(topCategory.id)) .stroke(Color.white, lineWidth: 2) ) .overlay(alignment: .topTrailing) { place.isFavorite ? Image(systemName: "heart.circle.fill") .symbolRenderingMode(.palette) .foregroundStyle(place.isFavorite ? .red : .black, place.isFavorite ? .white : .white) .font(.system(size: 24)) .shadow(radius: 5) .offset(x: 10, y: -10) : nil } } else { Image(systemName: "mappin.circle.fill") .frame(width: 30, height: 30) .aspectRatio(contentMode: .fit) .padding(10) .foregroundStyle(.white) .background(Color.red) .cornerRadius(30) } Image(systemName: "arrowtriangle.down.fill") .foregroundStyle(getColorForTopCategory(viewModel.categories.first(where: { $0.subCategoryIds.contains(place.subCategoryIds.first ?? 0) })?.id ?? 0)) .frame(height: 5) } .onTapGesture { selectedPlace = place isSheetPresented = true getlookAroundScene(for: place) } .contextMenu { ForEach(place.subCategoryIds.compactMap { subCategoryId in viewModel.subCategories.first(where: { $0.id == subCategoryId }) }, id: .id) { subCategory in Button("Show Only: (subCategory.name)") { selectedFilterCategory = subCategory.name } } ForEach(topCategories(for: place), id: .id) { category in Button("Show Only: (category.name)") { selectedFilterCategory = category.name } } } } } } </code>
Map(position: $position) {
    UserAnnotation()
    
    ForEach(filteredPlaces()) { place in
        Annotation("(place.name)n(getSubcategoryName(for: place))", coordinate: CLLocationCoordinate2D(latitude: place.latitude, longitude: place.longitude), anchor: .bottom) {
            VStack(spacing: 0) {
                Spacer()
                if let subCategoryId = place.subCategoryIds.first,
                   let subCategory = viewModel.subCategories.first(where: { $0.id == subCategoryId }),
                   let topCategory = viewModel.categories.first(where: { $0.subCategoryIds.contains(subCategoryId) }) {
                    Image(subCategory.imageUrl + "_icon")
                        .frame(width: 30, height: 30)
                        .aspectRatio(contentMode: .fit)
                        .padding(10)
                        .foregroundStyle(.white)
                        .background(
                            Circle()
                                .fill(getColorForTopCategory(topCategory.id))
                                .stroke(Color.white, lineWidth: 2)
                        )
                        .overlay(alignment: .topTrailing) {
                            place.isFavorite ? Image(systemName: "heart.circle.fill")
                                .symbolRenderingMode(.palette)
                                .foregroundStyle(place.isFavorite ? .red : .black, place.isFavorite ? .white : .white)
                                .font(.system(size: 24))
                                .shadow(radius: 5)
                                .offset(x: 10, y: -10) : nil
                        }
                } else {
                    Image(systemName: "mappin.circle.fill")
                        .frame(width: 30, height: 30)
                        .aspectRatio(contentMode: .fit)
                        .padding(10)
                        .foregroundStyle(.white)
                        .background(Color.red)
                        .cornerRadius(30)
                }
                Image(systemName: "arrowtriangle.down.fill")
                    .foregroundStyle(getColorForTopCategory(viewModel.categories.first(where: { $0.subCategoryIds.contains(place.subCategoryIds.first ?? 0) })?.id ?? 0))
                    .frame(height: 5)
            }
            .onTapGesture {
                selectedPlace = place
                isSheetPresented = true
                getlookAroundScene(for: place)
            }
            .contextMenu {
                ForEach(place.subCategoryIds.compactMap { subCategoryId in
                    viewModel.subCategories.first(where: { $0.id == subCategoryId })
                }, id: .id) { subCategory in
                    Button("Show Only: (subCategory.name)") {
                        selectedFilterCategory = subCategory.name
                    }
                }
                
                ForEach(topCategories(for: place), id: .id) { category in
                    Button("Show Only: (category.name)") {
                        selectedFilterCategory = category.name
                    }
                }
            }
        }
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật