0

I'm working in a real estate app like Zillow...I show a View with a list of properties and a map with annotations for each property.

I have an array of properties(each with lat/lon) and I calculate the MKCoordinateRegion after a MKLocalSearch. All good, but I want to filter to get ONLY the properties WITHIN the MKCoordinateRegion.

how can I do that?

function to reconcile the region

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

this is how I get my filtered listings

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

here is how I show my listings

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
                        }
                    }
                    
                }
            }
}
Nat Serrano
  • 472
  • 1
  • 4
  • 17
  • 1
    Please take the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/help/how-to-ask) to improve, edit and format your questions. Without a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) it is impossible to help you troubleshoot. – lorem ipsum Nov 24 '21 at 20:22
  • I understand. I was tempted to post the whole code but then some other people will complain that I posted too much code – Nat Serrano Nov 24 '21 at 20:31
  • 1
    You need a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) of what you have tried. The code you have now is overkill in its own way and you haven’t even posted how you tried to solve your problem. Do you expect someone recreate your code and solve your problem? – lorem ipsum Nov 24 '21 at 20:43
  • by popular demand I added the code in how I show the listings. but my question is simply how to filter annotations that lat/lon are not close to the MKregion – Nat Serrano Nov 24 '21 at 20:51
  • 1
    This isn’t the place to ask for free code. There are plenty of sites out there so you can pay someone to do just that. – lorem ipsum Nov 24 '21 at 20:55
  • I'm not asking for free code. – Nat Serrano Nov 24 '21 at 20:56
  • 1
    There is an instance method on MKMapView called [annotations(in:) ](https://developer.apple.com/documentation/mapkit/mkmapview/1452279-annotations) that returns this information. – Yrb Nov 26 '21 at 13:11
  • thank you @Yrb. I end up converting the MKCoordinateRegion to a CLCircularRegion and then use its contains(_:) method to check if the CLCoordinate2D is inside :) – Nat Serrano Nov 26 '21 at 16:48
  • It is a simple how to find a point within a rectangle. You have the `span` and the `center` in the `region`, so you can figure out the upper and lower bounds of the latitude and longitude and see if your coordinate is within those bounds. – lorem ipsum Nov 26 '21 at 20:20

0 Answers0