Я пытаюсь представить контроллер вида, когда пользователь нажимает на врага в 50 метрах от моего местоположения с помощью учебника от (https://www.raywenderlich.com/151817/how-to-make-an-app-like-pokemon-go).). Но мой viewcontroller не показывает, и я думаю, что я не вызываю метод делегата из расширения. Как сделать я чиню это?
Это мой код:
import UIKit
import MapKit
class GoogleMapsViewController: UIViewController, CLLocationManagerDelegate {
var targets = [Item]()
var locationManager: CLLocationManager!
var userLocation: CLLocation?
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.showsUserLocation = true
mapView.mapType = MKMapType(rawValue: 0)!
mapView.userTrackingMode = MKUserTrackingMode(rawValue: 2)!
setupLocations() // Do any additional setup after loading the view.
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
// Check for Location Services
if CLLocationManager.locationServicesEnabled() {
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
}
}
func setupLocations() {
let pdpk = Item( Place : "Pildammsparken", location: CLLocation(latitude: 55.590105, longitude: 12.988737))
targets.append(pdpk)
let btp = Item( Place : "Bulltoftaparken", location: CLLocation(latitude: 55.601326, longitude: 13.079005))
targets.append(btp) for item in targets {
let annotation = MapA(location: item.location.coordinate, item: item)
self.mapView.addAnnotation(annotation)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
defer { userLocation = locations.last }
if userLocation == nil {
// Zoom to user location
if let uLocation = locations.last {
let viewRegion = MKCoordinateRegionMakeWithDistance(uLocation.coordinate, 2000, 2000)
mapView.setRegion(viewRegion, animated: false)
print(uLocation)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension GoogleMapsViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
self.userLocation = userLocation.location
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
//1
let coordinate = view.annotation!.coordinate
//2
if let userCoordinate = userLocation {
//3
if userCoordinate.distance(from: CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)) < 50 {
//4
print("test")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let viewController = storyboard.instantiateViewController(withIdentifier: "ARViewController") as? PlaceViewController {
// more code later
//5
if let mapAnnotation = view.annotation as? MapA {
//6
self.present(viewController, animated: true, completion: nil)
}
}
}
}
}
}