gpt4 book ai didi

ios - 如何在 Xcode 的 map 中显示我当前的位置?

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

我在 YouTube 上观看了一些类(class),现在尝试获得实践经验。我正在进行一个小项目,其中涉及一个选项卡式应用程序,我尝试在第一页上创建一个带有显示当前位置的按钮的 map 。实际上很基本。但不知怎的,它就是不起作用,我不知道问题是什么。 CodewithChris 的某人告诉我:“我建议将您的应用程序分解为更小的组件,并确保每个组件都能正常工作,然后再继续下一个组件。尝试先输出您的位置,然后再将其绘制在 map 等上,这样您就可以更轻松地定位错误。”我只是不明白她所说的更小的组件是什么意思。我真的很感谢我能得到的所有帮助。下面是尽可能好的代码。预先感谢您的帮助。

import UIKit

import MapKit

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet var textFieldForAddress: UITextField!
@IBOutlet var getDirectionsButton: UIButton!
@IBOutlet var map: MKMapView!

var locationManger = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

locationManger.delegate = self
locationManger.desiredAccuracy = kCLLocationAccuracyBest
locationManger.requestAlwaysAuthorization()
locationManger.requestWhenInUseAuthorization()
locationManger.startUpdatingLocation()

map.delegate = self

}

@IBAction func getDirectionsTapped(_ sender: Any) {
getAddress()
}


func getAddress() {
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(textFieldForAddress.text!) { (placemarks, error) in
guard let placemarks = placemarks, let location = placemarks.first?.location
else {
print("No Location Found")
return
}
print(location)
self.mapThis(destinationCord: location.coordinate)

}
}

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

func mapThis(destinationCord : CLLocationCoordinate2D) {

let souceCordinate = (locationManger.location?.coordinate)!

let soucePlaceMark = MKPlacemark(coordinate: souceCordinate)
let destPlaceMark = MKPlacemark(coordinate: destinationCord)

let sourceItem = MKMapItem(placemark: soucePlaceMark)
let destItem = MKMapItem(placemark: destPlaceMark)

let destinationRequest = MKDirections.Request()
destinationRequest.source = sourceItem
destinationRequest.destination = destItem
destinationRequest.transportType = .automobile
destinationRequest.requestsAlternateRoutes = true

let directions = MKDirections(request: destinationRequest)
directions.calculate { (response, error) in
guard let response = response else {
if let error = error {
print("Something is wrong :(")
}
return
}

let route = response.routes[0]
self.map.addOverlay(route.polyline)
self.map.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)

}


}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let render = MKPolylineRenderer(overlay: overlay as! MKPolyline)
render.strokeColor = .blue
return render
}


}

最佳答案

如果要显示用户位置并让 map 跟踪它,则需要在 map View 上设置两个属性 - showsUserLocationuserTrackingMode。为了使 map 能够访问用户位置,您必须使用 CLocationManager 从用户那里接收 whenInUse 授权。

import MapKit

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

@IBOutlet var textFieldForAddress: UITextField!
@IBOutlet var getDirectionsButton: UIButton!
@IBOutlet var map: MKMapView!

var locationManger = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

locationManger.requestWhenInUseAuthorization()
map.showsUserLocation = true
map.userTrackingMode = .follow

}

如果您在模拟器上运行,则需要使用模拟器中的调试菜单来模拟位置。

关于ios - 如何在 Xcode 的 map 中显示我当前的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59912605/

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