斯威夫特放大用户的位置
问题描述:
我试图找出如何放大到用户的位置雨燕2.1。我搜遍了互联网的世界,但无法找到解决方案。谢谢Renier梅丽安帮助我开始这一点,但它并没有询问地点的授权,然后给出错误信息斯威夫特放大用户的位置
import Foundation
import MapKit
import CoreLocation
class SocialViewController : UIViewController {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
self.locationManager.requestWhenInUseAuthorization()
super.viewDidLoad()
self.mapView.showsUserLocation = true
self.mapView.userTrackingMode = .Follow
self.mapView.delegate = self
self.locationManager.startUpdatingLocation()
self.locationManager.delegate = self
}
}
extension SocialViewController : MKMapViewDelegate{
//Adjusting the zoom
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
var region = MKCoordinateRegion()
region.span = MKCoordinateSpanMake(0.7, 0.7); //Zoom distance
let coordinate = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
region.center = coordinate
mapView.setRegion(region, animated: true)
}
func mapViewWillStartLocatingUser(_ mapView: MKMapView) {
debugPrint("startLocating")
}
func mapViewDidStopLocatingUser(_ mapView: MKMapView) {
debugPrint("stopLocating")
}
}
extension SocialViewController: CLLocationManagerDelegate
{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
debugPrint("received Location")
}
}
答
尝试使用此代码,改变MKCoordinateSpanMake
值可以增加或减少放大
您viewDidLoad方法
import MapKit
import CoreLocation
class SocialViewController : UIViewController {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.showsUserLocation = true
self.mapView.userTrackingMode = .follow
self.mapView.delegate = self
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.locationManager.delegate = self
}
}
extension SocialViewController : MKMapViewDelegate{
//Adjusting the zoom
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
var region = MKCoordinateRegion()
region.span = MKCoordinateSpanMake(0.7, 0.7); //Zoom distance
let coordinate = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
region.center = coordinate
mapView.setRegion(region, animated: true)
}
func mapViewWillStartLocatingUser(_ mapView: MKMapView) {
debugPrint("startLocating")
}
func mapViewDidStopLocatingUser(_ mapView: MKMapView) {
debugPrint("stopLocating")
}
}
extension SocialViewController: CLLocationManagerDelegate
{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
debugPrint("received Location")
}
}
添加隐私键添加此行您info.plist中
工作!
希望这有助于
当我尝试这一点,说'不能分配型“SocialViewController”输入的值“MKMapViewDelgate?”' – salipshitz
@salipshitz再次检查我的回答,被编辑 –
你发当你写'MKMapViewDelgate'而不是'MKMapViewDelegate'时出现错字 – salipshitz