Search code examples
xcodemkmapviewswift4

Mkmapview - Putting an annotation on every grocery store within a specific region - SWIFT


I want to put an annotation on every grocery store within a specific area. I already have my mkmapview working and tracking my current location.

Thanks in advance!


Solution

  • Give it a try, unfortunately I'm not in situation to test it right now, but this piece of code should serve your requirement:

    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = "Groceries"
    request.region = mapView.region
    
    let search = MKLocalSearch(request: request)
        search.start(completionHandler: {(response, error) in
            if error != nil {
                print("Error occured in search: 
                \(error!.localizedDescription)")
            } else if response!.mapItems.count == 0 {
                print("No matches found")
            } else {
                print("Matches found")
    
                for item in response!.mapItems {
                    print("Name = \(item.name)")
                    print("Phone = \(item.phoneNumber)")
            }
        }
    })