gpt4 book ai didi

ios - 在 iOS 中放大用户位置

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

我已经尝试解决这个问题几个小时了,但我似乎无法找到解决方案。我正在尝试在我的 map View 上创建一个按钮,该按钮在按下时放大用户位置。以下是与按钮相关的功能代码:

func zoomInOnLocation() {
let userLocation = MKUserLocation()
let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()

let currentLocation: CLLocation? = userLocation.location
let latitude = currentLocation?.coordinate.latitude
let longitude = currentLocation?.coordinate.longitude
let span: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude!, longitude!)
let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
}

当我点击模拟器中的按钮时,我收到一条错误,指出 fatal error :

unexpectedly found nil while unwrapping an Optional value

第五行 mapDelegate.mapView!... 以红色突出显示。此外,我还向 Info.plist 添加了正确的标签。非常感谢任何帮助。

最佳答案

检查这个:

 if #available(iOS 9.0, *) {
locationManager.requestLocation()
} else {
// Fallback
}

let latitude:CLLocationDegrees = //insert latitutde

let longitude:CLLocationDegrees = //insert longitude

let latDelta:CLLocationDegrees = 0.05

let lonDelta:CLLocationDegrees = 0.05

let span = MKCoordinateSpanMake(latDelta, lonDelta)

let location = CLLocationCoordinate2DMake(latitude, longitude)

let region = MKCoordinateRegionMake(location, span)

mapView.setRegion(region, animated: false)

更多信息:Making the map zoom to user location and annotation (swift 2)

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

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