I am trying to refine a query of nearby placemarks. So far, all queries return the exact same 25 results. In the example below you can see 2 attempts (each done separately but displayed together for simplicity) labled "attempt 1" and "attempt 2"
func requestNearbyLocations() {
var region = MKCoordinateRegion()
region.center = CLLocationCoordinate2D(latitude: self.currentLocation.coordinate.latitude, longitude: self.currentLocation.coordinate.longitude)
let request = MKLocalPointsOfInterestRequest(center: region.center, radius: 10000.0)
//attempt 1
self.mapView.pointOfInterestFilter = .some(MKPointOfInterestFilter(including: []))
self.mapView.pointOfInterestFilter = .some(MKPointOfInterestFilter(excluding: [.restaurant, .cafe]))
//attempt 2
let categories : [MKPointOfInterestCategory] = [.cafe, .restaurant]
let filters = MKPointOfInterestFilter(excluding: categories)
self.mapView.pointOfInterestFilter = .some(filters)
let search = MKLocalSearch(request: request)
search.start { (response , error ) in
guard let response = response else {
return
}
print(response.mapItems)
for item in response.mapItems {
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
annotation.subtitle = item.phoneNumber
DispatchQueue.main.async {
self.mapView.addAnnotation(annotation)
}
}
}
}
How can I get the search to only return restaurants and cafes? I’d like to then use this to build more refined searches.
For example, the above search returned the below as one of the 25 entries in DC. I notice there is no "category" so how can it filter on a field that isn’t part of the query?:
<MKMapItem: 0x28052a490> {
isCurrentLocation = 0;
name = "National Cherry Blossom Festival";
phoneNumber = "+1 (877) 442-5666";
placemark = "National Cherry Blossom Festival, 2 15th St NW, Washington, DC 20024, United States @ <+38.88923000,-77.03530000> +/- 0.00m, region CLCircularRegion (identifier:'<+38.88923001,-77.03530000> radius 141.17', center:<+38.88923001,-77.03530000>, radius:141.17m)";
timeZone = "America/New_York (EST) offset -18000";
Also, it always returns 25 results. Is there a way to increase the results?
2
Answers
You’re setting the
pointOfInterestFilter
on themapView
when it should be set on the request.The
mapView
and therequest
are unrelated to each other, so setting the filters on one doesn’t affect the other.You are applying the filter to the wrong object. Apply the filter to the request, not the map.
https://developer.apple.com/documentation/mapkit/mklocalpointsofinterestrequest/3564414-pointofinterestfilter