gpt4 book ai didi

ios - 快速放大用户的位置

转载 作者:行者123 更新时间:2023-11-29 00:18:59 26 4
gpt4 key购买 nike

我想弄清楚如何在 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")
}
}

最佳答案

尝试使用此代码,更改 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 中添加私钥

enter image description here

工作!!

enter image description here

希望对你有帮助

关于ios - 快速放大用户的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44526301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com