0

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.

  • Shouldn't you set `searchRequest.region = mapView.region`? – Ricky Mo Jun 06 '23 at 02:39
  • Well, added this and now pins are showing up in completely different states than before OR where my location is so I guess that's a step in the right direction? – LiamBaynes85 Jun 06 '23 at 02:51
  • Maybe try `searchRequest.region = region` instead, since your `mapView.setRegion` is animated, `mapView.region` might not be completely updated by the time you set `searchRequest.region`. – Ricky Mo Jun 06 '23 at 03:03
  • Your code for setting up the location manager isn't really correct. See my answer at https://stackoverflow.com/a/74915706/20287183 for a better way to handle it. – HangarRash Jun 06 '23 at 03:05

0 Answers0