gpt4 book ai didi

ios - 用户位置不会出现在 Xcode 的模拟器中

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

我是应用程序开发的新手,我试图让用户位置出现在模拟器中,但我一直收到使用未解析的标识符错误。我查看了其他问题,这些问题非常针对他们自己的项目,并尝试以类似的方式处理我自己的应用程序但无济于事。有什么帮助吗?这是我的代码,以及指向 Xcode 屏幕截图的链接。

import UIKit
import MapKit
import CoreLocation

class SecondViewController: UIViewController, CLLocationManagerDelegate {

//Map
@IBOutlet weak var map: MKMapView!

let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{ let location = locations[0]

let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)

let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)


self.map.showsUserLocation = true

}

override func viewDidLoad()
{
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()

//This is where I get an error
map.setRegion(region, animated: true)**
}

Error: Use of unresolved identifier

screenshot

最佳答案

您的 let region:MKCoordinateRegion 是您的委托(delegate)方法中的本地标识符:

func locationManager(_:didUpdateLocations) {...}

这就是为什么您会使用未解析的标识符。让这个identifier在整个类中都可以访问,错误就会消失。喜欢:

class SecondViewController: UIViewController, CLLocationManagerDelegate {

//Map
@IBOutlet weak var map: MKMapView!
var region = MKCoordinateRegion()
.......
.......
}

注意:但这不会让您在 map 上看到任何东西。从您的 MapView 查看任何输出的最佳方式是将 map.setRegion(region, animated: true) 放入您的 delegate 方法中:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.first
let span = MKCoordinateSpanMake(0.01, 0.01)
let myLocation = CLLocationCoordinate2DMake((location?.coordinate.latitude)!, (location?.coordinate.longitude)!)
region = MKCoordinateRegionMake(myLocation, span)
map.setRegion(region, animated: true)
}

关于ios - 用户位置不会出现在 Xcode 的模拟器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44520377/

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