gpt4 book ai didi

ios - 在 viewDidLoad() 中使用 CLLocationManager() 可以防止 MKMapView 被拖动或缩放

转载 作者:行者123 更新时间:2023-11-28 08:05:21 25 4
gpt4 key购买 nike

使用以下代码拒绝拖动或放大/缩小 map View 。

我已经尝试了很多东西。我已经在属性检查器中允许所有必要的属性,甚至尝试添加 isZoomEnabled 和其他属性,但没有成功。

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet var latitudeLabel: UILabel!
@IBOutlet var longitudeLabel: UILabel!
@IBOutlet var courseLabel: UILabel!
@IBOutlet var speedLabel: UILabel!
@IBOutlet var altitudeLabel: UILabel!
@IBOutlet var addressLabel: UILabel!
@IBOutlet var mapView: MKMapView!

var manager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let location = locations[0]

self.latitudeLabel.text = String(location.coordinate.latitude)
self.longitudeLabel.text = String(location.coordinate.longitude)
self.courseLabel.text = String(location.course)
self.speedLabel.text = String(location.speed)
self.altitudeLabel.text = String(location.altitude)

let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
let latDelta: CLLocationDegrees = 0.10
let longDelta: CLLocationDegrees = 0.10
let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: longDelta)
let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: coordinates, span: span)

self.mapView.setRegion(region, animated: true)

CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
if error != nil {
print(error!)
}

else {
if let placemark = placemarks?[0] {
var address = ""

if placemark.subThoroughfare != nil {
address += placemark.subThoroughfare! + " "
}

if placemark.thoroughfare != nil {
address += placemark.thoroughfare! + "\n"
}

if placemark.subLocality != nil {
address += placemark.subLocality! + "\n"
}

if placemark.subAdministrativeArea != nil {
address += placemark.subAdministrativeArea! + "\n"
}

if placemark.postalCode != nil {
address += placemark.postalCode! + "\n"
}

if placemark.country != nil {
address += placemark.country! + " "
}

self.addressLabel.text = address

}
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

这段代码应该放在哪里,或者可以编辑什么来解决这个问题?

最佳答案

每次位置更新时都会设置 map View 的区域,从而阻止您以有意义的方式与 map 进行交互。

防止这种情况的一种方法是设置一个 Bool 指示该区域已经设置并且不再设置它。

var regionHasBeenSet = false

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// ...

if !regionHasBeenSet {
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
let latDelta: CLLocationDegrees = 0.10
let longDelta: CLLocationDegrees = 0.10
let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: longDelta)
let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: coordinates, span: span)

self.mapView.setRegion(region, animated: true)

regionHasBeenSet = true
}

CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in
// ...
}
}

如果您希望 map 的区域再次以该位置为中心,只需将 regionHasBeenSet 更改为 false 并且在下一次位置更新时该区域将被重置。

关于ios - 在 viewDidLoad() 中使用 CLLocationManager() 可以防止 MKMapView 被拖动或缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45336102/

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