j-ro / BusTrackDC-old

An app for iOS and Android to find out when your Metro or Circulator bus or Metro train is coming to a stop near you, using WMATA and NextBus prediction data.
http://bustrackdc.com
18 stars 8 forks source link

How to add multiple annotations to Mapkit? Not using Local Search request #64

Closed Gabidbm30 closed 5 years ago

Gabidbm30 commented 5 years ago

I'm making an app that shows all pizza places near your current location. I don't want to write all the annotations one by one because I want to show all the pizza places for a state. So I want to create a function that shows all the pizza places (not using local search). Using local search this would be the code: func performSearch() {

matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text
request.region = mapView.region

let search = MKLocalSearch(request: request)

search.start(completionHandler: {(response, error) in

    if let results = response {

        if let err = error {
            print("Error occurred in search: \(err.localizedDescription)")
        } else if results.mapItems.count == 0 {
            print("No matches found")
        } else {
            print("Matches found")

            for item in results.mapItems {
                print("Name = \(item.name ?? "No match")")
                print("Phone = \(item.phoneNumber ?? "No Match")")

                self.matchingItems.append(item as MKMapItem)
                print("Matching items = \(self.matchingItems.count)")

                let annotation = MKPointAnnotation()
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name
                self.mapView.addAnnotation(annotation)
            }
        }
    }
})

}

but I don't want to use local search because I want the annotations (pizza places) to appear by default (when I open the app I want the annotations to show). What function should I use?

j-ro commented 5 years ago

Hi there, I don't think I can really help you with this -- this is a project that's not related to pizza or maps directly. Best of luck!