Beginner here,
I'm building an app, for practice, that populates the map with businesses of a certain type (through MKLocalSearch request) within a 1000 meter radius.
My MKRegion is set right, my CLLocation variable is right, my annotations are pulling in wrong and I can't figure it out.
Wondering if it had to do with the fact that I tried to build the project before, using a GPX file set to be where the map annotations are now landing? I deleted the whole project and can't find any trace of it being linked.
Here is my code:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
mapView.delegate = self
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else { return }
print ("Location variable: \(location.coordinate)")
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
print("User's location - Latitude: \(latitude), Longitude: \(longitude)")
let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)
mapView.setRegion(region, animated: true)
print("User's location - Latitude: \(latitude), Longitude: \(longitude)")
print("Center of Region: \(region.center)")
locationManager.stopUpdatingLocation()
// Perform MapKit Search
let searchRequest = MKLocalSearch.Request()
searchRequest.naturalLanguageQuery = "Safeway"
let search = MKLocalSearch(request: searchRequest)
search.start { (response, error) in
guard let mapItems = response?.mapItems else {
print("Error: \(error?.localizedDescription ?? "")")
return
}
for mapItem in mapItems {
let annotation = MKPointAnnotation()
annotation.coordinate = mapItem.placemark.coordinate
// REPLACE ->
annotation.title = mapItem.name
annotation.subtitle = mapItem.phoneNumber
self.mapView.addAnnotation(annotation)
print("Annotation Coordinate: \(annotation.coordinate.latitude), \(annotation.coordinate.longitude), Placemark Coordinate: \(mapItem.placemark.coordinate.latitude), \(mapItem.placemark.coordinate.longitude)")
}
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { return nil }
let identifier = "CustomAnnotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
// Customize the marker appearance
if let markerAnnotationView = annotationView as? MKMarkerAnnotationView {
markerAnnotationView.markerTintColor = .red
}
return annotationView
}
}
I apologize for any formatting issues.
Tried checking all location settings and settings for any external packages. Set everything in my plist with privacy/location settings to be correct. Printed out variables throughout the process, as you can see, cannot figure out where it goes wrong.