我试图找出如何放大Swift 2.1中用户的位置。我搜索了互联网的世界,但无法找到解决方案。感谢Renier Melian帮助我开始这个,但它没有要求获得该位置的授权,然后给出错误消息
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")
}
}
答案 0 :(得分:1)
尝试使用此代码,更改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中添加隐私密钥
工作!!
希望这有帮助