在Apple Mapkit Swift上捏合和缩放



我已经能够在我的应用程序中实现MapKit,但它目前在一个静态位置查看地图。

我一直在努力添加捏合和缩放功能以及允许用户在地图上探索其他地区的能力。

这是我当前的代码的样子:

import UIKit
import MapKit
import CoreLocation
class HotPlacesViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
let newPin = MKPointAnnotation()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Setup the location services delegate in this class.
locationManager.delegate = self
// This little method requests the users permission for location services whilst in this view controller.
if CLLocationManager.authorizationStatus() == .notDetermined {
self.locationManager.requestAlwaysAuthorization()
let alert = UIAlertController(title: "You can change this option in the Settings App", message: "So keep calm your selection is not permanent. 🙂",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
// Drops the pin on the users current location.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
mapView.removeAnnotation(newPin)
let location = locations.last! as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
// Set the region on the map.
mapView.setRegion(region, animated: true)
newPin.coordinate = location.coordinate
mapView.addAnnotation(newPin)
}

}

我认为您的问题与此方法有关func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]),因为当位置更新时,您一遍又一遍地调整地图中的区域,这就是为什么您只看到位置更新的区域并且无法移动地图中的任何其他区域,您必须第一次制作标志并仅调整一次区域或根本不调整区域

编辑

import UIKit
import MapKit
import CoreLocation
class HotPlacesViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var isFirstTime = true
var locationManager = CLLocationManager()
let newPin = MKPointAnnotation()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Setup the location services delegate in this class.
locationManager.delegate = self
// This little method requests the users permission for location services whilst in this view controller.
if CLLocationManager.authorizationStatus() == .notDetermined {
self.locationManager.requestAlwaysAuthorization()
let alert = UIAlertController(title: "You can change this option in the Settings App", message: "So keep calm your selection is not permanent. 🙂",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
// Drops the pin on the users current location.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
mapView.removeAnnotation(newPin)
let location = locations.last! as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
if(self.isFirstTime){
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
// Set the region on the map.
mapView.setRegion(region, animated: true)
self.isFirstTime = false
}
newPin.coordinate = location.coordinate
mapView.addAnnotation(newPin)
}
}

希望这有帮助,如果我误解了您的问题,请告诉我

let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
mapView.setRegion(region, animated: true)

设置放大MapKit的范围

最新更新