我在像 Zillow 这样的房地产应用程序中工作......我显示了一个带有属性列表的视图和一个带有每个属性注释的地图。
我有一个属性数组(每个属性都有纬度/经度),我在 MKLocalSearch 之后计算 MKCoordinateRegion。一切都好,但我想过滤以仅获取 MKCoordinateRegion 内的属性。
我怎样才能做到这一点?
区域协调功能
func reconcileLocation(location: MKLocalSearchCompletion) {
let searchRequest = MKLocalSearch.Request(completion: location)
let search = MKLocalSearch(request: searchRequest)
search.start { (response, error) in
if error == nil, let coordinate = response?.mapItems.first?.placemark.coordinate {
self.coordinate = coordinate
self.region = MKCoordinateRegion(center: coordinate, span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03))
self.isLoading = false
}
}
}
这就是我获得过滤列表的方式
func getListingsFiltered(location: String, PriceMin: String, PriceMax: String, Bedrooms: Int ) {
var listingsF = [Listing]() //new array instance of listing
var lowestPrice = 0
var highestPrice = 1000000000
if PriceMin == "Any" {lowestPrice = 0} else {lowestPrice = Int(PriceMin) ?? 0}
if PriceMax == "Any" {highestPrice = 1000000000} else { highestPrice = Int(PriceMax) ?? 1000000000}
//append the ones that match
for listing in self.results!.listings {
//condition price
if Int(listing.price)! >= lowestPrice && Int(listing.price)! <= highestPrice {
//condition bedrooms
if Int(listing.bedrooms!)! >= Bedrooms {
//condition location
var l = Listing()
l.name = listing.name
l.neighborhood = listing.neighborhood
l.url = listing.url
l.price = listing.price
l.city = listing.city
l.state = listing.state
l.zipcode = listing.zipcode
//etc
listingsF.append(l)
}
}
}
DispatchQueue.main.async {
self.listingsF = listingsF
self.filters = true
}
}
这是我显示我的列表的方式
var body: some View {
NavigationView{
if !isMapShowing {
//show list
VStack {
//header
ZStack {
Rectangle()
.foregroundColor(Color.white)
//.cornerRadius(5)
.frame(height: 30)
HStack {
Spacer()
//button switch to map view
Button {
self.isMapShowing = true
} label: {
Image(systemName: "map")
.resizable()
.scaledToFit()
.frame(width: 30, height: 30, alignment: .center)
}.padding(.horizontal, 10).padding(.top).padding(.bottom,0)
//button switch to filter
NavigationLink {
FilterView()
//.navigationBarHidden(true)
} label: {
Image(systemName: "slider.horizontal.3")
.resizable()
.scaledToFit()
.frame(width: 30, height: 30, alignment: .center)
}.padding(.horizontal, 10).padding(.top).padding(.bottom,0)
}.padding()
}.padding(.top).padding(.bottom,0)
ScrollViewReader {ProxyReader in
ScrollView(.vertical, showsIndicators: false, content: {
//Listings
LazyVStack {
if model.results != nil || model.listingsF != nil {
if model.filters == nil || model.filters == false {
ForEach(model.results!.listings) { listing in
NavigationLink(destination: ListingDetailView(listing: listing)) {
VStack {
if listing.images?[0].image != nil {
ImageSliderView(images: listing.images ?? [])
.frame(height: 300)
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
}
Text(listing.name)
Text(listing.price)
Text(listing.neighborhood)
}
}
}
} else {
ForEach(model.listingsF!) { listing in
NavigationLink(destination: ListingDetailView(listing: listing)) {
VStack {
if listing.images?[0].image != nil {
ImageSliderView(images: listing.images ?? [])
.frame(height: 300)
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
}
Text(listing.name)
Text(listing.price)
Text(listing.neighborhood)
}
}
}
}
}
}
.id("SCROLL_TO_TOP")
.background(Color(.systemGroupedBackground))
})//scroll view
//to recreate the veiw from scratch
.id(self.scrollViewID)
}
}
.navigationBarTitle("")
.navigationBarHidden(true)
.ignoresSafeArea()
.onAppear {
if resultsFetched == false {
let listingsObtained = model.getListings()
if listingsObtained == true {
resultsFetched = true
}
}
}
}
}