I am working on a MapView with MapAnnotation on the Map and am having difficulty getting the MapAnnotation to function properly when selected. In my code, I want the MapAnnotation selected to be to the front if it is behind or partially blocked by another annotation. I have tried using a zIndex but this does not appear to work. Below is my MapView code along with a screenshot of the issue. Any help would be greatly appreciated.
struct HospitalMapView: View {
@StateObject var viewModel = HospitalViewModel()
@State private var showSearchView = false
@State private var showListView = false
@State private var selectedTab: Int = 0
var body: some View {
ZStack {
VStack(spacing: 0) {
TopTabView()
// Map that updates the visible hospitals when the user moves or adjusts the map
Map(coordinateRegion: $viewModel.region, interactionModes: .all, annotationItems: viewModel.filteredHospitals) { hospital in
MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: hospital.latitude, longitude: hospital.longitude)) {
RoundedRectangle(cornerRadius: 40)
.fill(viewModel.colorForPercentile(viewModel.calculatePercentile(for: hospital, in: viewModel.filteredHospitals)))
.frame(width: 70, height: 30) // Maintain consistent size, adjust if necessary
.overlay(
Text("$\(Int(hospital.baseCharge / 1000))K")
.foregroundColor(.white)
.bold()
)
.overlay(
RoundedRectangle(cornerRadius: 40)
.stroke(Color.blue, lineWidth: viewModel.selectedHospital == hospital ? 3 : 0)
)
.onTapGesture {
viewModel.selectHospital(hospital)
// Move the selected hospital to the end of the list to bring it to the front
viewModel.bringHospitalToFront(hospital)
viewModel.showBottomSheet = true
}
.scaleEffect(viewModel.selectedHospital == hospital ? 1.1 : 1.0) // Highlight the selected hospital
.zIndex(viewModel.selectedHospital == hospital ? 1 : 0) // Set the zIndex higher for the selected hospital
.animation(.easeInOut, value: viewModel.selectedHospital) // Smooth transition
}
}
.onAppear {
// Initial update of visible hospitals when the map appears
viewModel.updateFilteredHospitals()
}
.onChange(of: viewModel.region) { _ in
// Update the visible hospitals as the user changes the map region
viewModel.updateFilteredHospitals()
}
.edgesIgnoringSafeArea(.all)
}
// Hospital count display hovering over the map
Text("\(viewModel.filteredHospitals.count) Hospitals")
.font(.subheadline)
.foregroundColor(.white)
.padding(8)
.background(Color.black.opacity(0.6))
.cornerRadius(10)
.padding(.top, -315) // Adjust padding to position correctly below the TopTabView
.zIndex(1) // Ensure it's above the map
if let selectedHospital = viewModel.selectedHospital, viewModel.showBottomSheet {
BottomSheetView(isOpen: $viewModel.showBottomSheet, maxHeight: UIScreen.main.bounds.height * 0.3) {
SummaryTabView(selectedHospital: Binding(
get: { selectedHospital },
set: { newValue in viewModel.selectHospital(newValue) }
))
}
.transition(.move(edge: .bottom))
.animation(.spring())
}
}
.safeAreaInset(edge: .bottom) {
BottomTabView(selectedTab: $selectedTab, showListView: $showListView, showSearchView: $showSearchView,
onSearchSelected: {
showSearchView = true
showListView = false
}, onHomeSelected: {
showSearchView = false
showListView = false
}, onListSelected: {
showListView = true
showSearchView = false
})
}
.fullScreenCover(isPresented: $showSearchView) {
SearchView(viewModel: viewModel, showSearchView: $showSearchView, selectedTab: $selectedTab, showListView: $showListView)
}
.fullScreenCover(isPresented: $showListView) {
ListView(selectedTab: $selectedTab, showListView: $showListView, showSearchView: $showSearchView)
.environmentObject(viewModel)
}
.environmentObject(viewModel)
}
}
Post
Replies
Boosts
Views
Activity
Hi, I am fairly new Xcode/Swift and am trying to load a CSV File for use in my development. I have placed the CSV file in my Assets folder but when I try to create my Data Model and load the CSV file. I run into the error: No exact matches in call to initializer. Below is the code. I have attached CSV File. Any help fixing this error would be greatly appreciated. Thanks in advance for your help.
Brian
Hospital_Demographic_Data_Sample.csv
import Foundation
import CSV
struct HospitalData: Codable {
let providerNumber: String
let hospital: String
let address: String
let city: String
let state: String
let zip: String
let wageIndex: Double
let caseMix: Double
let averageCharge: Double
let discharges: Int
let totalCharges: Double
let adjTotalCharges: Double
// Add other fields as needed based on the columns in your CSV file
}
func loadHospitalData() -> [HospitalData]? {
guard let filePath = Bundle.main.path(forResource: "Hospital_Demographic_Data", ofType: "csv") else {
print("File not found")
return nil
}
do {
let csv = try CSV(url: URL(fileURLWithPath: filePath))
var hospitalDataList = [HospitalData]() // Initialize as an empty array
for row in csv.namedRows {
if let providerNumber = String(row["Provider CCN"] ?? ""), // Replace "Provider CCN" with actual column name
let hospital = String(row["Hospital Name"] ?? ""), // Replace with actual column name
let address = String(row["Street Address"] ?? ""),
let city = String(row["City"] ?? ""), // Replace with actual column name
let state = String(row["State Code"] ?? ""), // Replace with actual column name
let zip = String(row["Zip Code"] ?? ""), // Replace with actual column name
let wageIndex = Double(row["Wage Index"] ?? ""),
let caseMix = Double(row["Case Max"] ?? ""),
let averageCharge = Double(row["Base Charge"] ?? ""), // Replace with actual column name
let discharges = Int(row["Medicare Discharges"] ?? ""),
let totalCharges = Double(row["Total Charges"] ?? ""),
let adjTotalCharges = Double(row["Total Wage Normalized Charges"] ?? "") { // Replace with actual column name
let hospitalData = HospitalData(
providerNumber: providerNumber,
hospital: hospital,
address: address,
city: city,
state: state,
zip: zip,
wageIndex: wageIndex,
caseMix: caseMix,
averageCharge: averageCharge,
discharges: discharges,
totalCharges: totalCharges,
adjTotalCharges: adjTotalCharges
)
hospitalDataList.append(hospitalData)
}
}
return hospitalDataList
} catch {
print("Failed to load CSV file: \(error)")
return nil
}
}
// Usage Example
func main() {
if let hospitalData = loadHospitalData() {
for data in hospitalData {
print("Hospital: (data.hospital), City: (data.city), Average Charge: (data.averageCharge)")
}
}
}